Myślę, że trzeba dynamicznie generować plik robots.txt.
Powinieneś utworzyć RobotController do obsługi pliku robots.txt.
Check Reference Here
Podobne do powyższego linku było pytanie o umożliwiając rozszerzenie .txt na podawane przez działania: https://stackoverflow.com/a/14084127/511438
public ActionResult Robots()
{
Response.ContentType = "text/plain";
//-- Here you should write a response with the list of
//areas/controllers/action for search engines not to follow.
return View();
}
dodać Robots.cshtml
zmapować route, więc wywołanie pliku spowoduje wywołanie powyższej czynności.
routes.MapRoute("Robots.txt",
"robots.txt",
new { controller = "Home", action = "Robots" });
Oto atrybut NoRobots z kodem, aby uzyskać listę obszarów/kontrolerów/działań, które mają atrybut. Przepraszamy za interpretację pełnego tekstu obszaru nazw. Chciałbym, aby ktoś spojrzał na refleksję, aby lepiej pracować.
public sealed class NoRobotsAttribute : System.Attribute
{
public static IEnumerable<MethodInfo> GetActions()
{
return Assembly.GetExecutingAssembly().GetTypes()
.Where(t => (typeof(Controller).IsAssignableFrom(t)))
.SelectMany(
type =>
type.GetMethods(BindingFlags.Public | BindingFlags.Instance)
.Where(a => a.ReturnType == typeof(ActionResult))
);
}
public static IEnumerable<Type> GetControllers()
{
return Assembly.GetExecutingAssembly().GetTypes()
.Where(t => (typeof(Controller).IsAssignableFrom(t)));
}
public static List<string> GetNoRobots()
{
var robotList = new List<string>();
foreach (var methodInfo in GetControllers().Where(w => w.DeclaringType != null))
{
var robotAttributes = methodInfo
.GetCustomAttributes(typeof(NoRobotsAttribute), false)
.Cast<NoRobotsAttribute>();
foreach (var robotAttribute in robotAttributes)
{
//-- run through any custom attributes on the norobots attribute. None currently specified.
}
List<string> namespaceSplit = methodInfo.DeclaringType.FullName.Split('.').ToList();
var controllersIndex = namespaceSplit.IndexOf("Controllers");
var controller = (controllersIndex > -1 ? "/" + namespaceSplit[controllersIndex + 1] : "");
robotList.Add(controller);
}
foreach (var methodInfo in GetActions())
{
var robotAttributes = methodInfo
.GetCustomAttributes(typeof(NoRobotsAttribute), false)
.Cast<NoRobotsAttribute>();
foreach (var robotAttribute in robotAttributes)
{
//-- run through any custom attributes on the norobots attribute. None currently specified.
}
List<string> namespaceSplit = methodInfo.DeclaringType.FullName.Split('.').ToList();
var areaIndex = namespaceSplit.IndexOf("Areas");
var area = (areaIndex > -1 ? "/" + namespaceSplit[areaIndex + 1] : "");
var controllersIndex = namespaceSplit.IndexOf("Controllers");
var controller = (controllersIndex > -1 ? "/" + namespaceSplit[controllersIndex + 1] : "");
var action = "/" + methodInfo.Name;
robotList.Add(area + controller + action);
}
return robotList;
}
}
Zastosowanie:
[NoRobots] //Can be applied at controller or action method level.
public class HomeController : Controller
{
[NoRobots]
public ActionResult Index()
{
ViewData["Message"] = "Welcome to ASP.NET MVC!";
List<string> x = NoRobotsAttribute.GetNoRobots();
//-- Just some test code that wrote the result to a webpage.
return View(x);
}
}
... i dla pieszych.
namespace MVC.Temp.Areas.MyArea.Controllers
{
using MVC.Temp.Models.Home;
[NoRobots]
public class SubController : Controller
{
[NoRobots]
public ActionResult SomeAction()
{
return View();
}
}
}
Należy więc pamiętać, że rozwiązanie opiera się na przestrzeniach nazw iz przyjemnością przyjmie wszelkie ulepszenia, które ktoś może zaoferować.
Wreszcie należy poprawnie zapisać plik robota, w tym wszelkie informacje o nagłówku i obsługę symboli wieloznacznych.