The Sex and Cash Theory for Programmers

A URL Friendly Guid, but Not Shorter…

17. August 2010 13:35 by Scott in   //  Tags:   //   Comments (0)

I have a website that gets thousands upon thousands of hits a day.  And I use guids to identifiy everything.  So I needed something to show Guids in the URLS.  The best thing I found at that moment was Mads Kristensen’s Shorter URL Friendly Guid

Mads basically converts the Guid to base 64.  For a small website of sorts, this isn’t a bad idea.  But overtime and larger websites there will be more and more Guids represented.  After coming up with several errors in my site, I decided to investigate.  When converting to Base64 with Guids, I started to notice some slight loss of the Guid.  When I say slight, I mean one in every 2000 users had shown errors.  To me this isn’t acceptable by any means.  One user can destroy an entire website by talking slander so I was in need of a fix. 

So I threw out Mads idea and went searching for something different.  Its not uncommon to still use Guids in URL’s.  With the Dashes and all.  Microsoft, Flickr, Yahoo, Google, and some various other third party websites still use them in their URLs.  But I didn’t want the dashes.  I wanted just the numbers and letters.  Dashes could some day cause a problem, but not just numbers and letters.  So I decided to get rid of the dashes.  So here is the final code to use Guids as Urls.

I have a Guid extension class in C#.  One method is as follows:

public static string RemoveDashes(this Guid guid)
{
 return guid.ToString().Replace("-","");
}

This method gets called when ever I convert a Guid to a URL.  Whats nice about this, is the way I convert the Guid back with dashes.  No real fiddling with strings.  All that needs to be done is this:

Guid guid = new Guid("c9a646d39c614cb7bfcdee2522c8f633");

And that automatically puts the dashes back into the system.

If you liked this post, please be sure to subscribe to my RSS Feed.