Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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!

like image 823
barjed Avatar asked Dec 05 '25 23:12

barjed


1 Answers

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.

like image 62
Radu094 Avatar answered Dec 09 '25 16:12

Radu094



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!