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!
By
JulianSed
9 Mar 2017 11:47
Glum diamonds are everyone of the most <a href=http://joolwe.com/>joolwe.com</a> rare delusion color shades, eclipsed only by means of red and purple. In up to date months, two high-profile low-spirited diamonds have been sold at auction, which has invigorated the call for these incredibly first-rate and coveted gemstones. The sales generated a whirl of reports in media outlets 'round the world and much worldwide discussion.

They are <a href=http://joolwe.com/>joolwe.com</a> so rare that according to the Artless Color Diamond Association, on the contrary one unembellished pornographic diamond can be found on the exchange on account of every 100 Picasso paintings up at auction.

Add Comment

*
*
*
Captcha
*
*Required