asp.net-mvc – asp.net mvc排除来自搜索引擎抓取的动作
发布时间:2020-05-23 08:35:10 所属栏目:asp.Net 来源:互联网
导读:有没有办法从搜索引擎抓取中排除控制器操作?是否有MVC动词(属性),可以添加到动作名称上方? 我想从搜索引擎抓取中排除以下网址 Home/Secret?type=1 但我希望这可用于搜索引擎抓取 Home/Search 我认为您需要动态生成robots.txt文件. 您应该创建一个RobotContr
|
有没有办法从搜索引擎抓取中排除控制器操作?是否有MVC动词(属性),可以添加到动作名称上方? 我想从搜索引擎抓取中排除以下网址 Home/Secret?type=1 但我希望这可用于搜索引擎抓取 Home/Search 解决方法我认为您需要动态生成robots.txt文件.您应该创建一个RobotController来提供robots.txt文件. Check Reference Here 与上述链接相关的是关于允许.txt扩展由操作提供的问题: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();
}
添加Robots.cshtml 映射路由,以便对文件的调用将调用上面的操作. routes.MapRoute("Robots.txt","robots.txt",new { controller = "Home",action = "Robots" });
以下是NoRobots属性,其中包含用于获取具有该属性的区域/控制器/操作列表的代码.很抱歉解释完整的命名空间文本.希望有人能够看到反思,更好地解决问题. 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;
}
}
用法: [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);
}
}
……以及区域. namespace MVC.Temp.Areas.MyArea.Controllers
{
using MVC.Temp.Models.Home;
[NoRobots]
public class SubController : Controller
{
[NoRobots]
public ActionResult SomeAction()
{
return View();
}
}
}
所以请记住,解决方案依赖于命名空间,并欢迎任何人提供的改进. 最后,您需要正确编写机器人文件,包括任何标头信息和通配符支持. (编辑:安卓应用网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- asp.net-mvc – 异步操作方法
- asp.net-mvc – 如何通过Resharper 8.2.1停止INCORRECT_TYP
- asp.net-core – ASP.NET 5中RegisterObject / QueueBackgr
- ASP.NET成员提供程序 – 单一登录
- asp.net – 防止XSS(跨站脚本)
- asp.net – 用户控件的属性在回发后失去价值
- 在asp.net identity 2.0中获取分配角色的用户列表
- asp.net-mvc – ASP.NET MVC中的模型绑定嵌套集合
- asp.net-mvc – 如何将环境变量放在web.config中?
- asp.net – 网站无法启动另一个网站可能使用相同的端口
推荐文章
站长推荐
- asp.net – 找出.NET创建的HTML元素的客户端ID?
- asp.net – 何时覆盖OnError?
- asp.net-mvc – mvc 4 beginform with route nam
- 如何在asp.net按钮回发之前运行javascript函数
- asp.net-mvc – 如何对剃刀视图进行单元测试
- 将ASP.Net MVC的单页添加到现有的Web窗体应用程序
- 双应用程序域在ASP.NET 4应用程序
- asp.net – VS 2010 Web.config转换进行调试
- 在asp.net的内容页面中查找母版页内的UnorderedL
- asp.net – 没有使用WebApi委托处理程序分配内部
热点阅读
