Created
February 7, 2014 19:57
-
-
Save StevenClontz/8870552 to your computer and use it in GitHub Desktop.
Lowercase URLs in MVC 4
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// found in root folder \ | |
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Web; | |
using System.Web.Http; | |
using System.Web.Mvc; | |
using System.Web.Optimization; | |
using System.Web.Routing; | |
using System.Text.RegularExpressions; | |
namespace [...] | |
{ | |
[...] | |
public class MvcApplication : System.Web.HttpApplication | |
{ | |
[...] | |
// Sets up rewrite rule to force lowercase before the GET query string | |
protected void Application_BeginRequest(object sender, EventArgs e) | |
{ | |
string url = Request.Url.ToString(); | |
if (Request.HttpMethod == "GET" && Regex.Match(url, "(?<=^[^?]*)[A-Z]").Success) | |
{ | |
Response.RedirectPermanent(url.ToLower(), true); | |
} | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// found in folder \AppStart\ | |
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Web; | |
using System.Web.Mvc; | |
using System.Web.Routing; | |
namespace MvcMovie | |
{ | |
public class RouteConfig | |
{ | |
public static void RegisterRoutes(RouteCollection routes) | |
{ | |
// Enforces lower-case URLs on @Html.ActionLink links | |
routes.LowercaseUrls = true; | |
[...] | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment