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

2019-02-18 16:09发布

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!

1条回答
Summer. ? 凉城
2楼-- · 2019-02-18 16:48

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.

查看更多
登录 后发表回答