Lowercase Area Routes in ASP.NET MVC2


/ Published in: C#
Save to your folder(s)

Use it on your AreaRegistrationContext via the extension MapRouteLowercase


Copy this code and paste it in your HTML
  1. public static class LowercaseRouteExtensions
  2. {
  3. public static Route MapRouteLowercase(this RouteCollection routes, string name, string url, object defaults, object constraints, string[] namespaces)
  4. {
  5. if (routes == null)
  6. {
  7. throw new ArgumentNullException("routes");
  8. }
  9. if (url == null)
  10. {
  11. throw new ArgumentNullException("url");
  12. }
  13.  
  14. Route route = new LowercaseRoute(url, new MvcRouteHandler())
  15. {
  16. Defaults = new RouteValueDictionary(defaults),
  17. Constraints = new RouteValueDictionary(constraints),
  18. DataTokens = new RouteValueDictionary()
  19. };
  20.  
  21. if ((namespaces != null) && (namespaces.Length > 0))
  22. {
  23. route.DataTokens["Namespaces"] = namespaces;
  24. }
  25.  
  26. routes.Add(name, route);
  27.  
  28. return route;
  29. }
  30.  
  31. public static Route MapRouteLowercase(this AreaRegistrationContext @this, string name, string url, object defaults, object constraints, string[] namespaces)
  32. {
  33. if (namespaces == null && @this.Namespaces != null)
  34. {
  35. namespaces = @this.Namespaces.ToArray();
  36. }
  37.  
  38. var route = @this.Routes.MapRouteLowercase(name, url, defaults, constraints, namespaces);
  39. route.DataTokens["area"] = @this.AreaName;
  40.  
  41. var useNamespaceFallback = (namespaces == null || namespaces.Length == 0);
  42. route.DataTokens["UseNamespaceFallback"] = useNamespaceFallback;
  43.  
  44. return route;
  45. }
  46. }
  47.  
  48. public class LowercaseRoute : Route
  49. {
  50. public LowercaseRoute(string url, IRouteHandler routeHandler)
  51. : base(url, routeHandler)
  52. {
  53. }
  54.  
  55. public LowercaseRoute(string url, RouteValueDictionary defaults, IRouteHandler routeHandler)
  56. : base(url, defaults, routeHandler)
  57. {
  58. }
  59.  
  60. public LowercaseRoute(string url, RouteValueDictionary defaults, RouteValueDictionary constraints, IRouteHandler routeHandler)
  61. : base(url, defaults, constraints, routeHandler)
  62. {
  63. }
  64.  
  65. public LowercaseRoute(string url, RouteValueDictionary defaults, RouteValueDictionary constraints, RouteValueDictionary dataTokens, IRouteHandler routeHandler)
  66. : base(url, defaults, constraints, dataTokens, routeHandler)
  67. {
  68. }
  69.  
  70. public override VirtualPathData GetVirtualPath(RequestContext requestContext, RouteValueDictionary values)
  71. {
  72. var path = base.GetVirtualPath(requestContext, values);
  73.  
  74. if (path != null)
  75. path.VirtualPath = path.VirtualPath.ToLowerInvariant();
  76.  
  77. return path;
  78. }
  79. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.