Tutorial :ASP.Net MVC - route object id == title - how to deal with duplicates?


Web pages have moved to use URLs like:


i.e. they include the title of the page in the url rather than having some coded id.

I understand that this is useful for SEO, and also for users in finding the correct page where they wish to type in the url.

I would like to follow this approach, but wonder how best to acheive it, and particularly how to deal with duplicates.

Is a database trigger which creates the url based on the title and adds a numeric incremental suffix to any duplicates the best way to go, and if so what would such a trigger look like?


In Wordpress at least, the "slug" (as they call it) is generated once from the item's title and stored separately in the database. If two "slugs" collide, it appends -1, -2, etc. to the end. I personally prefer if you add an (optional) field to the submission form to allow people to insert their ownâ€"it allows people to specify a shorter URL than my-long-article-is-hard-to-type.


Instead of having an id based on a title they could use id based on both a date and a title (2007/12/03/asp-net-mvc-framework-part-2-url-routing). So if you don't have articles with the same titles in one day (which isn't too severe restriction) duplicates are eliminated.


You've got to model this concept in your application. URL generation based on title can be automatic, but it can't be invisible. WordPress (and probably other CMS's, too) do a pretty good job of this -- they'll default a URL based on the information you enter, but the "key" part of the URL is visible and editable to the user, and uniqueness is enforced at the appropriate level (globally, per month, per day -- whatever).

Making URL generation completely invisible will lead to confusing errors for the user, I believe.


You could do the same thing that SO does. That is, the slug is only there as GoogleJuice. These two URLs resolve to the same thing:

ASP.Net MVC - route object id == title - how to deal with duplicates?

ASP.Net MVC - route object id == title - how to deal with duplicates?

So, in the example you gave, if the CMS gave each post a unique numeric identifier (which I suppose is quite likely) then you can include it in the URL:


In this example, the symbol 1234 is the post's identifier.

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Next Post »