Geeks With Blogs

Bill Osuch - Random geek notes

Search engines hate query strings.

For example, say you have a website that sells widgets. You have four types of widgets: red, green, blue and black. You have a page titled Inventory.aspx that accepts a query string to show each of the widgets, like this:

To make this more SEO friendly, you could have a URL like this:

That allows a search engine to index the site easier, and to more easily identify relevant content for search queries.

The old way would have been to actually create those subdirectories and drop a default page into each one, but by using URL Routing you can create an "alias" that the framework can decipher behind the scenes and direct to the correct page.

You'll add a new route to the Global.asax file. Start by adding a using directive:

using System.Web.Routing;

In the Application_Start method, call a new method, passing it a RouteCollection:


Then add that method:

void RegisterRoutes(RouteCollection routes)
     routes.MapPageRoute(routeName, routeUrl, physicalFile);

The parameters are:

  • routeName - a friendly name for the route
  • routeUrl - the new URL pattern than the route will use
  • physicalFile - the original aspx page where you passed the querystring

So for our example above, it would look like:

routes.MapPageRoute("Widgets", "Widgets/{WidgetType}", "~/Widgets.aspx");

That's all that's needed in Global.asax to handle the routing; now you'll construct the new URLs. You could hardcode the URL, like:

<asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl="~/Widgets/Red" Text="All Red Widgets" />

Or you could build the URL using a RouteURL expression:

<asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl="<%$RouteUrl:WidgetType=Red" Text="All Red Widgets" />

Finally, you have to have a way to retrieve the data that you were previously passing via querystring. You can do this via the Page.RouteData property:


However, you probably don't want to completely replace the querystring functionality on the landing page - what if a user has a bookmark containing the querystring? Something like this would take care of both situations:

string widgetType = "";

if (Page.RouteData.Values.Count() > 0)
     widgetType  = Page.RouteData.Values["WidgetType"].ToString();
     widgetType  = Request.QueryString["Type"];

Technorati Tags: , - Your tech ebook super store

Posted on Monday, May 12, 2014 12:05 PM C# , ASP.Net | Back to top

Comments on this post: ASP.Net sites, Clean URLs, and URL Routing

# re: ASP.Net sites, Clean URLs, and URL Routing
Requesting Gravatar...
thanks a lot it is well written article helped a lot

thanks again
Left by Anurag on Jul 17, 2015 11:22 AM

# re: ASP.Net sites, Clean URLs, and URL Routing
Requesting Gravatar...
Interesting Topic. I have always looked for something clear to use and still can't find it.

I am new to So unfortunately your explanation is not quite clear. You should have explain more clearly how to apply everything, from creating the Global.asax class, and where to write everything you're explaining.

Conclusion. I am still lost. Thank you anyway.
Left by Tom on Mar 23, 2016 3:49 PM

# re: ASP.Net sites, Clean URLs, and URL Routing
Requesting Gravatar...
Nice and well informative article. it helped me a lot.
Left by Vinod on Jul 02, 2016 7:35 AM

# re: ASP.Net sites, Clean URLs, and URL Routing
Requesting Gravatar...
Thank you for this article. It seems simple, but I am still stuck at the last several lines of code. Where should I put the group lines of code that contains widgetType = Page.RouteData.Values["WidgetType"].ToString(); ? Is it also inside the "RegisterRoutes" method?

Also the why I got an error saying "Page" does not exist in current context? How can I fix this?
Left by Marvin on Nov 02, 2016 9:06 PM

Your comment:
 (will show your gravatar)

Copyright © Bill Osuch | Powered by: