微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

asp.net-mvc – asp.net mvc排除来自搜索引擎抓取的动作

有没有办法从搜索引擎抓取中排除控制器操作?是否有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();
        }

    }
}

所以请记住,解决方案依赖于命名空间,并欢迎任何人提供的改进.

最后,您需要正确编写机器人文件,包括任何标头信息和通配支持.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐