我应该如何忽略URL asp.net MVC中的сhar?

时间:2022-09-11 10:27:48

Please, give me an advice on the following question: there's a usual string like here : /Search/Index, and a user inputs a char accidentally: /Search/Index'. How should I ignore the сhar and make a user simply follow the link Search/Index?

请给我一个关于以下问题的建议:这里有一个常用字符串:/ Search / Index,用户意外输入一个字符:/ Search / Index'。我应该如何忽略сhar并让用户只需点击链接搜索/索引?

2 个解决方案

#1


3  

you can add this code block in your Global.asax file.

您可以在Global.asax文件中添加此代码块。

The line "{controller}/{action*}/{id}" have {action*} so action will be index and any characters after index will consider as ignored and route to the index action.

“{controller} / {action *} / {id}”行有{action *},因此action将是index,index之后的所有字符都将被视为忽略并路由到index操作。

public static void RegisterRoutes(RouteCollection routes) {
    routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

    routes.MapRoute(
        "Default", // Route name
        "{controller}/{action*}/{id}", // URL with parameters
        new { controller = "Search", action = "Index", id = UrlParameter.Optional } // Parameter defaults
    );
}

#2


0  

I think this is not advisable. This would result in multiple urls for the same ressource and your searchengine ranking might suffer. It might be better to just return a 404

我认为这是不可取的。这将导致同一个资源的多个网址,并且您的搜索引擎排名可能会受到影响。返回404可能会更好

http://moz.com/blog/canonical-url-tag-the-most-important-advancement-in-seo-practices-since-sitemaps

http://moz.com/blog/canonical-url-tag-the-most-important-advancement-in-seo-practices-since-sitemaps

#1


3  

you can add this code block in your Global.asax file.

您可以在Global.asax文件中添加此代码块。

The line "{controller}/{action*}/{id}" have {action*} so action will be index and any characters after index will consider as ignored and route to the index action.

“{controller} / {action *} / {id}”行有{action *},因此action将是index,index之后的所有字符都将被视为忽略并路由到index操作。

public static void RegisterRoutes(RouteCollection routes) {
    routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

    routes.MapRoute(
        "Default", // Route name
        "{controller}/{action*}/{id}", // URL with parameters
        new { controller = "Search", action = "Index", id = UrlParameter.Optional } // Parameter defaults
    );
}

#2


0  

I think this is not advisable. This would result in multiple urls for the same ressource and your searchengine ranking might suffer. It might be better to just return a 404

我认为这是不可取的。这将导致同一个资源的多个网址,并且您的搜索引擎排名可能会受到影响。返回404可能会更好

http://moz.com/blog/canonical-url-tag-the-most-important-advancement-in-seo-practices-since-sitemaps

http://moz.com/blog/canonical-url-tag-the-most-important-advancement-in-seo-practices-since-sitemaps