Skip to content

Instantly share code, notes, and snippets.

@StevenClontz
Created February 7, 2014 19:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save StevenClontz/8870552 to your computer and use it in GitHub Desktop.
Save StevenClontz/8870552 to your computer and use it in GitHub Desktop.
Lowercase URLs in MVC 4
// 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);
}
}
}
}
// 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