URL Rewriting with ASP.Net
March 15, 2006
I received the following question via email the other day. Brian said I should post my response since I hadn’t posted anything in a while. Hope it helps someone!
I need to intercept some URLs. All URLs to “/this/url” need to go to “this_page.aspx” no matter if they’re “/this/url/is/cool” or “/this/url/is/not”. In Apache world, I’d use a RewriteRule.
What do we do in .Net? I’ve found potential solutions in the Web.config — HttpHandlers and such.
But I don’t so much want to write a handler. I really just want to “catch” URLs and send them to a certain page.
What’s the best way?
You are getting into one of my personal pet peeves with asp.net. You would think that popularity, functionality, power and flexibility of mod_rewrite would have prompted MS to bring that same functionality over to ASP a long time ago. Especially considering how long it has been around. I think it is one of those things that they just don’t “get” - like why they shouldn’t mess with my HTML. I believe ASP.Net 2.0 has better built-in capabilities that I have not experienced first-hand, but I have heard that it still isn’t nearly as powerful as mod_rewrite.
If you own the server that you are deploying to, I would recommend Helicon Tech’s ISAPI_Rewrite. It implements the majority of the same functionality as mod_write but uses a slightly different syntax. It appears to just be one guy, but he’s been around a while (forum goes back to 2002) and he is good about responding in the threads. His pay product supports ISP functionality so that each account can have their own rules file, but if it is just you, you can manage a number of sites from one server with one rule file. That is what we do on our server that hosts FearlessDog / SwoofWare / ThinkFirst / etc. This solution is application independent too. The sites I just listed are all WordPress installs. I did a proof of concept a while back for MyHomePoint. We can’t move forward with clean urls for MyHomePoint though because our host doesn’t support it.
If you don’t own the server, then that presents a bit of a problem. As you mentioned, you can do a lot with handlers and there are some projects out there (CodeProject?) that take you the majority of the way there. However, you run into a variety of unseen issues (postbacks, resource files - images, styles, etc.). James Shaw had a brief post referencing several of the popular solutions. Of course, his conclusion was to not mess with it since there is no clean way to do it.
One of the biggest problems with the handler approach is that it is usually an all or nothing approach. In order for the handler to work, the request needs to get handled by the asp.net engine. To make that happen, you either need to intercept everything and then exclude stuff like images, stylesheets, scripts, etc. Or you put a .aspx extension on everything so that it is forced into the asp.net engine. In the asp.net world, these are clean urls:
- http://www.myhomepoint.com/forums/13/ShowForum.aspx
- http://www.myhomepoint.com/forums/thread/161.aspx
Those qualify as clean urls, right? Unfortunately, they aren’t platform agnostic which I believe is also one of the goals of url rewriting. Without the extension, it just assumes you are asking for a folder and then looks for a default.aspx file. If that isn’t found, then you are out of luck. Some solutions I’ve seen involve pre-defining your folder hierarchy and putting a default.aspx file in each folder. Needless to say, that approach has limitations.
You may consider looking at Community Server to see how they handle the problem with handlers. You can download the app and source. Looking at their code is probably a good thing to do regardless since you can get some good ideas on how to deal with other stuff.
While it is a lofty goal to achieve, my advice is that if you don’t own the server or have a host that supports the ISAPI_Rewrite component, then don’t try to implement it. You will burn far too many billable hours solving the inevitable quirks that pop up.
Hope that helps!
Popularity: 11% [?]
Comments
3 Responses to “URL Rewriting with ASP.Net”
Got something to say?
Community Server Daily News for Thursday, June 1, 2006
Today’s top news item is a big congratulations to Kevin Harder, an all-around White Knight and Coding
Matt of SwoofWare discusses URL Rewriting
Matt of SwoofWare discusses URL Rewriting and near the end of his post gives some very good advice, “You
[…] Matt of SwoofWare discusses URL Rewriting Matt of SwoofWare discusses URL Rewriting and near the end of his post gives some very good advice, “You may consider looking at Community Server to see how they handle the problem with handlers. You can download the app and source. Looking at their code is probably a good thing to do regardless since you can get some good ideas on how to deal with other stuff.” Published Thursday, June 01, 2006 8:54 AM by daveburke […]