Click here to Skip to main content
15,891,529 members
Articles / All Topics

SEO Friendly URL Routing in ASP.NET

Rate me:
Please Sign up or sign in to vote.
5.00/5 (4 votes)
7 Mar 2015Ms-PL1 min read 16.1K   4   1
SEO Friendly URL Routing in ASP.NET

I stumbled upon this while answering to this in stackoverflow. So thought of sharing how to do this ASP.NET webproject.

Expectation / End Result

  1. If you have Search.aspx file in project, it will automatically become SEO friendly, i.e., you can browse it like this “/Search” instead of “/Search.aspx”.
  2. If you need to pass parameters to that page, say product name, then you can do that using “/Search/Kindle” instead of “/Search.aspx?productname=Kindle”.

Steps to achieve this are listed below:

Step 1

Install “Microsoft.AspNet.FriendlyUrls” from nuget package.

Open package manager console – Help. Then type the following:

Install-Package Microsoft.AspNet.FriendlyUrls
Step 2

Then it will automatically add the following in RouteConfig.cs.

C#
public static class RouteConfig
{
   public static void RegisterRoutes(RouteCollection routes)
   {
      var settings = new FriendlyUrlSettings();
      settings.AutoRedirectMode = RedirectMode.Permanent;
      routes.EnableFriendlyUrls(settings); 
    }
}
Step 3

Add a webform with name say “Search.aspx”. And now if you browse http://www.example.com/Search, it will hit “Search.aspx”.

Now you are done with making SEO friendly URLS.

More Customization

Part – 1

If you want to make Search.aspx to be called as “Search-Product”, then you can do that using the following:

C#
routes.MapPageRoute("", "Search-Product", "~/Search.aspx");

You need to add this to RouteConfig.cs, just after “routes.Enable…”

Now, if you hit this URL – http://www.example.com/search-product, it will hit search.aspx.

Part -2

Now, you may need to pass parameters to Search.aspx. Yes you can do that, use the following line instead of the above.

C#
routes.MapPageRoute("Find", "Search-product/{productname}", "~/Search.aspx");

To get value of productname in Search.aspx, use the following “Page.RouteData.Values[“productname”]” in page_load or any other event in Search.aspx.

Example

I have created an example. Use the code suggested above. Hit the following URL:

Image 1

Output in code is as follows:

Image 2

Hope it works for all those following this blog.

Image 3 Image 4

License

This article, along with any associated source code and files, is licensed under The Microsoft Public License (Ms-PL)


Written By
Software Developer
India India
Developing web based application since last 5 years, also interested in designing application with optimized used of available tech stacks, as well as apply those and real life experience to help out friends, colleagues,communities such as this or other forums.

MCSD (2013) certified developer.

Reach me - http://about.me/arindamnayak

Comments and Discussions

 
QuestionRouting in WEB API ( FRIENDLY URL ) Pin
Member 1391272815-Jul-18 20:23
Member 1391272815-Jul-18 20:23 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.