asp.net mvc从搜索引擎抓取中排除某个操作

时间:2013-08-11 21:36:21

标签: asp.net-mvc asp.net-mvc-3 asp.net-mvc-4 seo

有没有办法从搜索引擎抓取中排除Controller操作?是否有 MVC 动词(属性),可以添加到动作名称上方?

我想从搜索引擎抓取中排除以下网址

Home/Secret?type=1

但我希望这可用于搜索引擎抓取

Home/Search

4 个答案:

答案 0 :(得分:10)

我认为您需要动态生成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();
        }

    }
}

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

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

答案 1 :(得分:2)

如果它可以公开访问,特别是在页面上链接,机器人可以/将会找到它。您可以在链接上使用rel="nofollow",使用页面本身上的noindex meta tag,或使用robots.txt文件对页面进行Disallow索引。这将阻止所有诚实的搜索引擎(如谷歌,必应,雅虎)索引或关注链接,但不会阻止随机机器人查看页面。

从来没有,公众可以访问URL。人类可以访问它,然后计算机就可以访问它。如果您想阻止普通大众访问它,您可能希望查看用户身份验证。

答案 2 :(得分:0)

可能你只需要改变路由。您可以添加以下路线。它会将地址Home/Secret?type=1更改为Home/Search

routes.MapRoute(
                name: "NewRoute",
                url: "{controller}/Search",
                defaults: new { controller = "Home", action = "Secret", type = UrlParameter.Optional }
            );

您也可以隐藏控制器名称:

routes.MapRoute(
                name: "NewRoute",
                url: "LadyGaga/Search",
                defaults: new { controller = "Home", action = "Secret", type = UrlParameter.Optional }
            );

答案 3 :(得分:0)

您想要将其隐藏在搜索引擎中,还是没有人可以访问该网址?因为任何请求您的robots.txt的人都会在那里找到这些网址。

难道你不能只授权只允许某些用户访问这些操作吗?当提供HTTP 401时,搜索引擎不会将其编入索引。