Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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