how to generate dynamic url using .NET MVC


I am creating new .NET MVC web apllication and to make it's url seo friendly i would like to create dynamic url's based on post title etc.
how can i do it in .NET MVC

Also i believe, i would have to change routing also using RouteConfig.cs 

Any help would be appreciated, thanks


Asked by:- neena
1
: 9861 At:- 5/16/2017 7:34:21 AM
.net MVC C# Seo







2 Answers
profileImage Answered by:- vikas_jk

Hello, thank you for asking your question 

I will clear all the things step by step :-

  1. yes you can create dynamic url easily with .NET MVC, for this  i suggest you to add one more column in your table which save's dynamic generated slug url from title of your post.
    -> Create a Helper class for generating seo friendly url
    //Pass your title as Name in this url, and get generated slug 
    public static string GenerateSlug(string Name)
            {
                string phrase = string.Format("{0}", Name);
    
                string str = RemoveAccent(phrase).ToLower();
                // invalid chars           
                str = Regex.Replace(str, @"[^a-z0-9\s-]", "");
                // convert multiple spaces into one space   
                str = Regex.Replace(str, @"\s+", " ").Trim();
                // cut and trim 
                str = str.Substring(0, str.Length <= 60 ? str.Length : 60).Trim();
                str = Regex.Replace(str, @"\s", "-"); // hyphens   
                return str;
            }
    // About getEnocding https://msdn.microsoft.com/en-us/library/system.text.encodinginfo.getencoding(v=vs.110).aspx
      private static string RemoveAccent(string text)
            {
                byte[] bytes = System.Text.Encoding.GetEncoding("Cyrillic").GetBytes(text);
                return System.Text.Encoding.ASCII.GetString(bytes);
            }
    
  2. From the abve code you will get generate slug, save it in databse.
  3. Now When you show the post url in html/razor, paste it like:-
    https://yourwebsite.com/category/{generated-slug}/{id}
  4. To make this work, yes you would have to change RouteConfig.cs
  5. Go to RouteConfig.cs placed in App_Start folder of your MVC application, now place this code above Default Route
  routes.MapRoute(
              name: "Test", //Route Name
              url: "category/{generatedSlug}/{id}",// dynamic URL
              defaults: new { controller = "Post", action = "PostDetailsAction", generatedSlug = UrlParameter.Optional,id=UrlParameter.Optional }
          );
//action = method which generates post's data based on id from database

that's it, you are done :)

 

1
At:- 5/16/2017 8:00:53 AM Updated at:- 1/20/2018 8:01:32 AM
Thank you for quick answer, work's well for me 0
By : neena - at :- 5/16/2017 8:06:45 AM


 method RemoveAccent not have ?

0
At:- 5/16/2017 8:42:57 AM
Thanks to point that out, Nguy?n , updated my answer 0
By : vikas_jk - at :- 5/16/2017 8:46:51 AM






Login/Register to answer
Or
Register directly by posting answer/details

Full Name *

Email *




By posting your answer you agree on privacy policy & terms of use