在启用了LowercaseUrls的路由参数中保留案例

时间:2013-03-13 13:53:57

标签: c# asp.net-mvc asp.net-mvc-4

我在我的MVC 4应用程序中使用routes.LowercaseUrls = true;,该应用程序运行良好。但是,参数也会降低,所以如果我有一个像

这样的路线
routes.MapRoute(
    name: "MyController",
    url: "foo/{hash}/{action}",
    defaults: new { controller = "MyController", action = "Details" }
);

使用

生成的链接
@Html.ActionLink("my link", "Details", new { hash=ViewBag.MyHash })

也会将{hash} - 部分网址缩小,例如,如果ViewBag.MyHash = "aX3F5U",则生成的链接将为/foo/ax3f5u而不是/foo/aX3F5U

有没有办法强制MVC只小写控制器和动作部分?

对于旧版本的MVC,要走的路似乎是实现Route的自定义子类,但我不知道如何/在何处实例化它,因为路由构造函数的签名是相当的与MapRoute不同,我希望有一个更简单的方法。

4 个答案:

答案 0 :(得分:2)

我认为使用自定义子类Route的解决方案将足够简单,但同时又有点难看:)

您可以CustomRoute方法RegisterRoute添加RouteConfig.cs。添加以下代码而不是routes.MapRoute

var route = new CustomRoute(new Route(
  url: "{controller}/{action}/{id}",
  defaults: new RouteValueDictionary() { 
    { "controller", "Home" }, 
    { "action", "Index" }, 
    { "id", UrlParameter.Optional }
  },
  routeHandler: new MvcRouteHandler()
));
routes.Add(route);

特定CustomRoute的实现可能如下所示:

public class CustomRoute : RouteBase
{
  private readonly RouteBase route;

  public CustomRoute(RouteBase route)
  {
    this.route = route;
  }

  public override VirtualPathData GetVirtualPath(RequestContext requestContext, RouteValueDictionary values)
  {
    values = new RouteValueDictionary(values.Select(v =>
    {
      return v.Key.Equals("action") || v.Key.Equals("controller")
        ? new KeyValuePair<String, Object>(v.Key, (v.Value as String).ToLower())
        : v;
    }).ToDictionary(v => v.Key, v => v.Value));

    return route.GetVirtualPath(requestContext, values);
  }

  public override RouteData GetRouteData(HttpContextBase httpContext)
  {
    return route.GetRouteData(httpContext);
  }
}

然而,这不是最佳实施。完整示例可以使用RouteCollection上的扩展名和自定义Route子项的组合,以使其尽可能接近原始routes.MapRoute(...)语法:

LowercaseRoute类:

public class LowercaseRoute : Route
{
    public LowercaseRoute(string url, IRouteHandler routeHandler) : base(url, routeHandler) { }

    public override VirtualPathData GetVirtualPath(RequestContext requestContext, RouteValueDictionary values)
    {
        values = new RouteValueDictionary(values.Select(v =>
        {
            return v.Key.Equals("action") || v.Key.Equals("controller")
              ? new KeyValuePair<String, Object>(v.Key, (v.Value as String).ToLower())
              : v;
        }).ToDictionary(v => v.Key, v => v.Value));

        return base.GetVirtualPath(requestContext, values);
    }
}

RouteCollectionExtensions类:

public static class RouteCollectionExtensions
{
    [SuppressMessage("Microsoft.Design", "CA1054:UriParametersShouldNotBeStrings", MessageId = "2#", Justification = "This is not a regular URL as it may contain special routing characters.")]
    public static Route MapLowercaseRoute(this RouteCollection routes, string name, string url)
    {
        return MapLowercaseRoute(routes, name, url, null /* defaults */, (object)null /* constraints */);
    }

    [SuppressMessage("Microsoft.Design", "CA1054:UriParametersShouldNotBeStrings", MessageId = "2#", Justification = "This is not a regular URL as it may contain special routing characters.")]
    public static Route MapLowercaseRoute(this RouteCollection routes, string name, string url, object defaults)
    {
        return MapLowercaseRoute(routes, name, url, defaults, (object)null /* constraints */);
    }

    [SuppressMessage("Microsoft.Design", "CA1054:UriParametersShouldNotBeStrings", MessageId = "2#", Justification = "This is not a regular URL as it may contain special routing characters.")]
    public static Route MapLowercaseRoute(this RouteCollection routes, string name, string url, object defaults, object constraints)
    {
        return MapLowercaseRoute(routes, name, url, defaults, constraints, null /* namespaces */);
    }

    [SuppressMessage("Microsoft.Design", "CA1054:UriParametersShouldNotBeStrings", MessageId = "2#", Justification = "This is not a regular URL as it may contain special routing characters.")]
    public static Route MapLowercaseRoute(this RouteCollection routes, string name, string url, string[] namespaces)
    {
        return MapLowercaseRoute(routes, name, url, null /* defaults */, null /* constraints */, namespaces);
    }

    [SuppressMessage("Microsoft.Design", "CA1054:UriParametersShouldNotBeStrings", MessageId = "2#", Justification = "This is not a regular URL as it may contain special routing characters.")]
    public static Route MapLowercaseRoute(this RouteCollection routes, string name, string url, object defaults, string[] namespaces)
    {
        return MapLowercaseRoute(routes, name, url, defaults, null /* constraints */, namespaces);
    }

    [SuppressMessage("Microsoft.Design", "CA1054:UriParametersShouldNotBeStrings", MessageId = "2#", Justification = "This is not a regular URL as it may contain special routing characters.")]
    public static Route MapLowercaseRoute(this RouteCollection routes, string name, string url, object defaults, object constraints, string[] namespaces)
    {
        if (routes == null)
        {
            throw new ArgumentNullException("routes");
        }
        if (url == null)
        {
            throw new ArgumentNullException("url");
        }

        Route route = new LowercaseRoute(url, new MvcRouteHandler())
        {
            Defaults = CreateRouteValueDictionary(defaults),
            Constraints = CreateRouteValueDictionary(constraints),
            DataTokens = new RouteValueDictionary()
        };

        if ((namespaces != null) && (namespaces.Length > 0))
        {
            route.DataTokens["Namespaces"] = namespaces;
        }

        routes.Add(name, route);

        return route;
    }

    private static RouteValueDictionary CreateRouteValueDictionary(object values)
    {
        var dictionary = values as IDictionary<string, object>;
        if (dictionary != null)
        {
            return new RouteValueDictionary(dictionary);
        }

        return new RouteValueDictionary(values);
    }
}

您现在可以使用MapLowercaseRoute代替MapRoute,所以

routes.MapRoute(
    name: "MyController",
    url: "foo/{hash}/{action}",
    defaults: new { controller = "MyController", action = "Details" }
);

简单地变成

routes.MapLowercaseRoute(
    name: "MyController",
    url: "foo/{hash}/{action}",
    defaults: new { controller = "MyController", action = "Details" }
);

暴露所需的行为。

答案 1 :(得分:2)

这是一种简单的方法,

public class MyRoute : Route
{
    public MyRoute(string url, object defaults): base(url, new RouteValueDictionary(defaults), new MvcRouteHandler())
    { 
    }

    public override VirtualPathData GetVirtualPath(RequestContext requestContext, RouteValueDictionary values)
    {
        if (values["action"] != null)
            values["action"] = values["action"].ToString().ToLowerInvariant();
        if (values["controller"] != null)
            values["controller"] = values["controller"].ToString().ToLowerInvariant();
        return base.GetVirtualPath(requestContext, values);
    }
}

routes.Add("Default",new MyRoute("{controller}/{action}/{id}",
                new { controller = "Home", action = "Index", id = MyUrlParameter.Optional }));

有关详细信息,请参阅this blog post

答案 2 :(得分:0)

如果您查看私有RouteCollection.NormalizeVirtualPath方法,您会看到它只使用virtualPath.ToLowerInvariant()。所以没有办法解决这个问题。即使你创建了自己的路线,它也会被小写。

但你可以做的是在'#'符号之后添加哈希,即"foo/{action}/#{hash}"。我没有  尝试过,但它应该工作。只需看看NormalizeVirtualPath实施。

答案 3 :(得分:-1)

这很简单,如1.2.3,

看一下这个例子

   routes.MapRouteLowercase( // changed from routes.MapRoute
       "Default",
       "{controller}/{action}/{id}",
       new { controller = "Home", action = "Index", id = UrlParameter.Optional }
   );

通过Nuget下载和安装它很简单,我使用它。 的 PM> Install-Package LowercaseRoutesMVC

http://lowercaseroutesmvc.codeplex.com/