SEO Friendly Slug URLs Extension Method

10 Oct

Here is a quick Extension Method that you can use to generate a friendly string for use in a URL.

Function ToFriendlyUrl(txt As String, Optional maxLength As Integer = 45) As String
If String.IsNullOrWhiteSpace(txt) Then

Return String.Empty
Dim str As String = RemoveAccent(txt).ToLower()

str = RegularExpressions.Regex.Replace(str, "[^a-z0-9\s-]", String.Empty)
str = RegularExpressions.Regex.Replace(str, "\s+", " ").Trim()
str = str.Substring(0, If(str.Length str = RegularExpressions.Regex.Replace(str, "\s", "-")

Return str

End If

End Function

we use Regular expressions to only allow letters and dashes, so now you can create SEO Friendly URL for your site just by using this extension method on any URL String.

Leave a comment

Posted by on 10, Oct 2011 in .NET, ASP.NET, MVC, VB.NET


Tags: , , ,

add your comment

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: