ASP.NET 4.0 URL Rewriting: How to deal with the IDs

All we need is an easy explanation of the problem, so here it is.

I have just started adding the new .NET 4.0 URL Rewriting into my project. I have a question.

Let’s say I have a Article.aspx that displays, well, articles. I made a route for it in the Global.asax:

routes.MapPageRoute("article-browse", "article/{id}", "~/Article.aspx");

So the link consists of the article’s id which is, obviously, not a very nice, nor SEO friendly link. I would like to display the Article’s title in the link, instead of the ID.

Do I have to pass the whole title in the parameter (instead of the id) and then make a SQL query that searches for a database record with the matching title? That sounds scary. Maybe there is some way to do something similar to the Eval() methods, that would change the title into an ID?

Thank you very much!

How to solve :

I know you bored from this bug, So we are here to help you! Take a deep breath and look at the explanation of your problem. We have many solutions to this problem, But we recommend you to use the first method because it is tested & true method that will 100% work for you.

Method 1

There is nothing to prevent you from including both the ID (for quick SQL retrieval) and the article’s title in the link (for SEO purposes). This is exactelly how stackoverflow is handling the routing (check the address for this question).

routes.MapPageRoute("article-browse", "article/{id}/{title}", "~/Article.aspx");

Obviously, the title after the ID is not necessary to display the page (you only use the ID to fetch the article), but everytime you generate the link in your site, generate it with the title, and the bots will use that when indexing your pages.

Oh, and you might also want to create a method that translates your title into a URL-friendly string.Like making all lowercase, converting spaces and other characters to ‘-‘,etc.

Note: Use and implement method 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply