Avoid uppercase letters in your Episerver page URL

For a number of reasons it's widely recommended to use all lowercase URL's inside a web site. Unfortunately Episerver out of the box uses the same casing as the entered PageName (unless you modify the URL manually).

In order to always get a lowercase URL here's some code we've been using for ages. Hook it up on the SavingPage and CreatingPage events and make your web site a little bit better.

using EPiServer.Web;
if (string.IsNullOrEmpty(page.URLSegment))
    page.URLSegment = UrlSegment.CreateUrlSegment(page);

if (!string.IsNullOrEmpty(page.URLSegment))
    page.URLSegment = page.URLSegment.ToLower();

if (page.URLSegment.Contains("--"))
    page.URLSegment = Regex.Replace(page.URLSegment, "--+", "-");

UPDATE 1: Check out the CreatedUrlSegment event.

UPDATE 2: Check out Joel's blog post for a complete example compatible with Episerver 7.

Published and tagged with these categories: Episerver, ASP.NET