Urlorize Extensions

Urlorize converts strings to Url friendly strings.

public static class UrlorizeExtensions
{
    private readonly static Regex cleaner = new Regex("[^a-z0-9 ]+", RegexOptions.IgnoreCase);
    private readonly static Regex spaceMinimizer = new Regex("[ ]+", RegexOptions.IgnoreCase);
 
    public static string Urlorize(this HtmlHelper helper, string value)
    {
        value = cleaner.Replace(value, " ");
        value = spaceMinimizer.Replace(value, " ");
        value = value.Replace(" ", "-");
        return value.ToLower();
    }
}

Usage:

<%=Html.Urlorize("test test test") %>

0 Responses to “Urlorize Extensions”


  • No Comments

Leave a Reply