Home > Uncategorized > Simple friendly urls in ASP.Net

Simple friendly urls in ASP.Net

August 26, 2005

With the channel 9 redesign we’ve added some friendly url support such as http://channel9.msdn.com/niners/itprochris for niner profiles and http://channel9.msdn.com/tags/Windows+Vista for tags. This allows for urls which are much easier to remember and discover.

So the question was asked on Channel9 "How did we do it?". We looked into creating HttpHandlers and even ISAPI filters. But this was all a little over board for what we needed.

Then on a long flight across America, I found a simple solution in the August issue of asp.netPRO magazine. (Actually I just noticed their website is done with classic ASP, that’s funny. But hey, its a quality magazine with good articles, so who cares 🙂 Unfortunately I don’t know the name of the author of the tip, but I’ll explain it here.

  1. Create a folder inside your web application root. We’ll use "tags" in this example.
  2. In the tags folder create a default.aspx page ( we’ll come back to the code for that page later ).
  3. Next in IIS find the "tags" folder and go to Properties > Custom Errors.
  4. Select HTTP Error 404 and then Edit Properties… Change Message Type to Url and then the value of the Url to "/tags/default.aspx". (Don’t forget to add the virtual folder if you are inside of one, ex. /channel9/tags/default.aspx)
  5. Now when a request is made for "/tags/asp.net", IIS will raise the 404 and transfer to your default.aspx page. Then in the Page_Load event of the default.aspx page you can parse the Request.RawUrl, to extract the "asp.net" tag from the requested url.
  6. From there you can return the content you need based on the extracted tag and the url appears exactly as the user requested.

Simple yet effective.

Downloadable Code

%d bloggers like this: