[MVC]Lowercase Route URLs in ASP.NET MVC

转载:http://goneale.com/2008/12/19/lowercase-route-urls-in-aspnet-mvc/

Lowercase Route URL's in ASP.NET MVC

For anybody interesting in having ruby on rails style routes; or follow a lowercase URL pattern (mysite.com/member/profile/goneale) which most web 2.0 sites seem to be following, check out the below code cited and enhanced from this article (http://www.makiwa.com/index.php/2008/05/31/lowercase-mvc-route-urls/).

Firstly you will need a RouteExtensions.cs file, or named anything you like with the following (compatible as at ASP.NET MVC RC1):

  1. using System;
  2. using System.Web.Mvc;
  3. using System.Web.Routing;
  4.  
  5. namespace MyMvcApplication.App.Helpers
  6. {
  7.     public class LowercaseRoute : System.Web.Routing.Route
  8.     {
  9.         public LowercaseRoute(string url, IRouteHandler routeHandler)
  10.             : base(url, routeHandler) { }
  11.         public LowercaseRoute(string url, RouteValueDictionary defaults, IRouteHandler routeHandler)
  12.             : base(url, defaults, routeHandler) { }
  13.         public LowercaseRoute(string url, RouteValueDictionary defaults, RouteValueDictionary constraints, IRouteHandler routeHandler)
  14.             : base(url, defaults, constraints, routeHandler) { }
  15.         public LowercaseRoute(string url, RouteValueDictionary defaults, RouteValueDictionary constraints, RouteValueDictionary dataTokens, IRouteHandler routeHandler)
  16.             : base(url, defaults, constraints, dataTokens, routeHandler) { }
  17.  
  18.         public override VirtualPathData GetVirtualPath(RequestContext requestContext, RouteValueDictionary values)
  19.         {
  20.             VirtualPathData path = base.GetVirtualPath(requestContext, values);
  21.  
  22.             if (path != null)
  23.                 path.VirtualPath = path.VirtualPath.ToLowerInvariant();
  24.  
  25.             return path;
  26.         }
  27.     }
  28.  
  29.     public static class RouteCollectionExtensions
  30.     {
  31.         public static void MapRouteLowercase(this RouteCollection routes, string name, string url, object defaults)
  32.         {
  33.             routes.MapRouteLowercase(name, url, defaults, null);
  34.         }
  35.  
  36.         public static void MapRouteLowercase(this RouteCollection routes, string name, string url, object defaults, object constraints)
  37.         {
  38.             if (routes == null)
  39.                 throw new ArgumentNullException("routes");
  40.  
  41.             if (url == null)
  42.                 throw new ArgumentNullException("url");
  43.  
  44.             var route = new LowercaseRoute(url, new MvcRouteHandler())
  45.             {
  46.                 Defaults = new RouteValueDictionary(defaults),
  47.                 Constraints = new RouteValueDictionary(constraints)
  48.             };
  49.  
  50.             if (String.IsNullOrEmpty(name))
  51.                 routes.Add(route);
  52.             else
  53.                 routes.Add(name, route);
  54.         }
  55.     }
  56. }

Then a using reference in your Global.asax.cs file to the above class, and you’re all set to create a lowercase route.
You can see a below example of a lowercase route and anytime this route is called your URL will be lowercased.

  1.             routes.MapRouteLowercase(
  2.                 "Default", // Route name
  3.                 "{controller}/{action}/{id}", // URL with parameters
  4.                 new {controller = "Home", action = "index", id = ""} // Parameter defaults
  5.                 );

and optionally if you are interested in converting any incoming URL’s to lowercase (manually typed by the user or called links) you can use this in your Application_BeginRequest() method (Remember, this is not needed for lowercase routes themselves, the code above will handle that):

  1.         protected void Application_BeginRequest(Object sender, EventArgs e)
  2.         {
  3.             // If upper case letters are found in the URL, redirect to lower case URL.
  4.             // Was receiving undesirable results here as my QueryString was also being converted to lowercase.
  5.             // You may want this, but I did not.
  6.             //if (Regex.IsMatch(HttpContext.Current.Request.Url.ToString(), @"[A-Z]") == true)
  7.             //{
  8.             // string LowercaseURL = HttpContext.Current.Request.Url.ToString().ToLower();
  9.  
  10.             // Response.Clear();
  11.             // Response.Status = "301 Moved Permanently";
  12.             // Response.AddHeader("Location", LowercaseURL);
  13.             // Response.End();
  14.             //}
  15.  
  16.             // If upper case letters are found in the URL, redirect to lower case URL (keep querystring the same).
  17.             string lowercaseURL = (Request.Url.Scheme + "://" + HttpContext.Current.Request.Url.Authority + HttpContext.Current.Request.Url.AbsolutePath);
  18.             if (Regex.IsMatch(lowercaseURL, @"[A-Z]"))
  19.             {
  20.                 lowercaseURL = lowercaseURL.ToLower() + HttpContext.Current.Request.Url.Query;
  21.  
  22.                 Response.Clear();
  23.                 Response.Status = "301 Moved Permanently";
  24.                 Response.AddHeader("Location", lowercaseURL);
  25.                 Response.End();
  26.             }
  27.         }
赞(0) 打赏
分享到: 更多 (0)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏