Thursday, 03 February 2005
An ISAPI Filter to have nice URLs for your Asp.Net site
My content management system stores all the pages in a sql database, so if you go to /home.html, there is no document by that name on the server. Instead, i have one .aspx page called content.aspx that handles every request dynamically and it outputs the content for the page.
You can partially achieve this same effect without using an ISAPI filter (see http://www.codeproject.com/aspnet/URLRewriter.asp) but you don't get the advantage of specifying a folder path as a valid url. For example, if you request /Departments/HR/ then you will get a 404 on the server, because IIS doesn't know to pass on this request to my /content.aspx page. Jakob Nielsen (the usability expert) says that hackable urls are important, i.e. users should be allowed to remove parts of the path of the page they are at, and arrive at a level higher up in the site. The ISAPI filter used here allows this.
Requirements for the Filter
- Every .html request should be forwarded to /content.aspx
- Every default document request should be forwarded to /content.aspx
- A request with a querystring (?) should not be redirected because it is assumed to be a dynamic page.
- The IIS log should record the original url (i.e. /Home.html) instead of /content.aspx?Url=/home.html
Because the code is in C++ and there is a lot of messy error checking stuff going on all over the place, i won't post it here (mess up my blog). instead you can download the VS 2003 solution (20 k) with source files + executable, and examine it for yourself if you're interested. It does work, and i have about 15 sites runnning off this filter for a long time now.
This solution is 95% taken from David Wang's excellent post on the ISAPI-DEV ms newsgroup. I just added in support for default documents and ignoring dynamic page requests.
Thursday, 03 February 2005 12:14:02 (GMT Standard Time, UTC+00:00) Asp.Net