Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>If you're just looking to support lower-case routes (basically making your routes case-insensitive), you might check out the below. We're currently using this and it works great.</p> <p>Firstly you will need a RouteExtensions.cs file, or named anything you like with the following (compatible as at ASP.NET MVC RC1):</p> <pre><code>using System; using System.Web.Mvc; using System.Web.Routing; namespace MyMvcApplication.App.Helpers { public class LowercaseRoute : System.Web.Routing.Route { public LowercaseRoute(string url, IRouteHandler routeHandler) : base(url, routeHandler) { } public LowercaseRoute(string url, RouteValueDictionary defaults, IRouteHandler routeHandler) : base(url, defaults, routeHandler) { } public LowercaseRoute(string url, RouteValueDictionary defaults, RouteValueDictionary constraints, IRouteHandler routeHandler) : base(url, defaults, constraints, routeHandler) { } public LowercaseRoute(string url, RouteValueDictionary defaults, RouteValueDictionary constraints, RouteValueDictionary dataTokens, IRouteHandler routeHandler) : base(url, defaults, constraints, dataTokens, routeHandler) { } public override VirtualPathData GetVirtualPath(RequestContext requestContext, RouteValueDictionary values) { VirtualPathData path = base.GetVirtualPath(requestContext, values); if (path != null) path.VirtualPath = path.VirtualPath.ToLowerInvariant(); return path; } } public static class RouteCollectionExtensions { public static void MapRouteLowercase(this RouteCollection routes, string name, string url, object defaults) { routes.MapRouteLowercase(name, url, defaults, null); } public static void MapRouteLowercase(this RouteCollection routes, string name, string url, object defaults, object constraints) { if (routes == null) throw new ArgumentNullException("routes"); if (url == null) throw new ArgumentNullException("url"); var route = new LowercaseRoute(url, new MvcRouteHandler()) { Defaults = new RouteValueDictionary(defaults), Constraints = new RouteValueDictionary(constraints) }; if (String.IsNullOrEmpty(name)) routes.Add(route); else routes.Add(name, route); } } } </code></pre> <p>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.</p> <pre><code> routes.MapRouteLowercase( "Default", // Route name "{controller}/{action}/{id}", // URL with parameters new {controller = "Home", action = "index", id = ""} // Parameter defaults ); </code></pre> <p>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):</p> <pre><code> protected void Application_BeginRequest(Object sender, EventArgs e) { // If upper case letters are found in the URL, redirect to lower case URL. // Was receiving undesirable results here as my QueryString was also being converted to lowercase. // You may want this, but I did not. //if (Regex.IsMatch(HttpContext.Current.Request.Url.ToString(), @"[A-Z]") == true) //{ // string LowercaseURL = HttpContext.Current.Request.Url.ToString().ToLower(); // Response.Clear(); // Response.Status = "301 Moved Permanently"; // Response.AddHeader("Location", LowercaseURL); // Response.End(); //} // If upper case letters are found in the URL, redirect to lower case URL (keep querystring the same). string lowercaseURL = (Request.Url.Scheme + "://" + HttpContext.Current.Request.Url.Authority + HttpContext.Current.Request.Url.AbsolutePath); if (Regex.IsMatch(lowercaseURL, @"[A-Z]")) { lowercaseURL = lowercaseURL.ToLower() + HttpContext.Current.Request.Url.Query; Response.Clear(); Response.Status = "301 Moved Permanently"; Response.AddHeader("Location", lowercaseURL); Response.End(); } } </code></pre> <p>Answer originally came from <a href="https://stackoverflow.com/questions/878578/how-can-i-have-lowercase-routes-in-asp-net-mvc">this</a> SO post.</p> <p>The content on this answer was taken from a blog post that is no longer available but can be viewed on archive.org <a href="http://web.archive.org/web/20100726101930/http://goneale.com/2008/12/19/lowercase-route-urls-in-aspnet-mvc" rel="nofollow noreferrer">here</a>.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload