ASP.NET 4.0 URL redirection: how to deal with identifiers

I just started adding a new URL to the .NET 4.0 URL in my project. I have a question.

Say I have Article.aspx that displays, well, articles. I made a route for him in Global.asax:

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

Thus, the link consists of an article identifier, which is obviously not very pleasant, and not an SEO-friendly link. I would like to display the title of the article in the link, not the identifier.

Should I pass the entire header in the parameter (instead of the identifier), and then make an SQL query that looks for the database record with the corresponding header? That sounds scary. Maybe there is a way to do something similar to the Eval () methods that would change the title to ID?

Many thanks!

+3
source share
1 answer

You have nothing to interfere with including both the identifier (for quick SQL search) and the article title in the link (for SEO purposes). This is exactly how stackoverflow handles routing (check the address for this question).

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

Obviously, the title after the ID is not needed to display the page (you only use the ID to retrieve the article), but every time you create a link on your site, generate it with a title, and bots will use that when indexing your pages.

Oh, and you can also create a method that translates your title into a URL-friendly string. Just like all lowercase letters, convertible spaces and other characters to "-", etc.

+5
source

Source: https://habr.com/ru/post/1793461/


All Articles