How can I avoid duplicate content in ASP.NET MVC due to case-insensitive URLs and defaults?

前端 未结 7 1165
无人及你
无人及你 2020-12-24 03:22

Edit: Now I need to solve this problem for real, I did a little more investigation and came up with a number of things to reduce duplicat

7条回答
  •  粉色の甜心
    2020-12-24 03:53

    As well as posting here, I emailed ScottGu to see if he had a good response. He gave a sample for adding constraints to routes, so you could only respond to lowercase urls:

    public class LowercaseConstraint : IRouteConstraint
    {
        public bool Match(HttpContextBase httpContext, Route route,
                string parameterName, RouteValueDictionary values,
                RouteDirection routeDirection)
        {
            string value = (string)values[parameterName];
    
            return Equals(value, value.ToLower());
        }
    

    And in the register routes method:

    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
    
        routes.MapRoute(
            "Default",                                              // Route name
            "{controller}/{action}/{id}",                           // URL with parameters
            new { controller = "home", action = "index", id = "" },
            new { controller = new LowercaseConstraint(), action = new LowercaseConstraint() }
        );
    }
    

    It's a start, but 'd want to be able to change the generation of links from methods like Html.ActionLink and RedirectToAction to match.

提交回复
热议问题