{%Pragmatic Coding%} Tips and tricks for software developers.


C# ASP.NET friendly “slug” url creating

Hello friends!

I had to develop asp.net mvc controller which could accept a parameter as a slug string for friendly URL's which look like the following:

First what should be done is adding new MapRote with modified url template and parameters:

Great, we now have formatted our route for blog record url. Now we need to wtite extension method which will generate clear formatted URL.

This method will return well formatted string you can use it inside url. And how to use it, please, see below:

That's was pretty simple. I hope it was helpful for you!

Share on social network

Share to Facebook
Share to Google Plus
Share to LiveJournal
Share to MyWorld
Share to Odnoklassniki
Share to Yandex
Comments (2) Trackbacks (0)
  1. str = Regex.Replace(str, @”[\s-]+”, ” “).Trim();
    would be replaced with
    str = Regex.Replace(str, @”[\s-]{2,}”, ” “).Trim();
    This modofication will skip single spaces and single hyphens.

Leave a comment

No trackbacks yet.