Simple friendly urls in ASP.Net
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.
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.
- Create a folder inside your web application root. We’ll use "tags" in this example.
- In the tags folder create a default.aspx page ( we’ll come back to the code for that page later ).
- Next in IIS find the "tags" folder and go to Properties > Custom Errors.
- 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)
- 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.
- 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.