Vanity URLs

By
Dave
Project
Published
15 Apr 2011 11:31
Last Modified
13 Jan 2013 18:15

The category controller currently uses the following URL structure:

http://{domain}/blog/category/{slug1}

It is often useful for URLs to be more concise, for example when adding a caption to a video. In such cases "vanity URLs" are often used. I've updated the blog categories to enable them to support a vanity URL, currently based on the category slug, as follows:

http://{domain}/blog/{slug}

The approach I took was to inject specific entries into the MVC routing table. Clearly this wouldn't be sensible for large numbers of entries, but the number of blog cetegories should be relatively small, and not every category has to have one. An added complication is that whenever I add, update, or delete a category I may need to regenerate the routing table to support adding/removing vanity URLs. I add the enries as follows:

foreach (Category category in new BlogService().ListCategories())
   if (category.VanityUrl)
      routes.MapRoute(category.Slug, category.Slug, new { controller = "Archive", action = "CategoryIndex", name = category.Slug });

Listing 1. Adding vanity URLs to MVC route table

1 A slug is a "friendly" URL (both from an SEO and human-perspective), using characters which do not require HTML-encoding (e.g. dashes instead of spaces)

Comments

By
Davidvut
8 Feb 2017 16:47
Anabolic Steroids Shop - Buy Injectable and Oral Bodybuilding Drugs Online http://etalaze.org - Show more!

Add Comment

*
*
*
Captcha
*
*Required