Up till now we've been rewriting URL's using a custon 404 page: the url would not map to any file in the site, and we configured the IIS to send 404 error to a aspx page which redirected those url's to the correct URL.
Now we want to stop using redirects, so after reading Scott Guthrie's article on Url Rewriting, I want to use the Application_BeginRequest in Global.asax. The thing is that a lot of our url's are not rewrites, and can get to the right place without any intervention. I'm worried that now every single request is going to have to go through the Application_BeginRequest method (even the un-rewritten url's), and I'm afraid it will slow down their loading time.
What do you think? Is loading time an issue when using Application_BeginRequest?
views:
3492answers:
5Every request goes through Application_BeginRequest anyway.
You'll need to add some logic so only those pages that need to be rewritten are changed.
That small bit of logic won't be very expensive.
I've used it, and didn't notice performance suffering at all.
Scott Guthrie's article is a good one, but I am curious as to why you are choosing to do this via the Global.asax instead of using an HttpModule as he suggests. Also, the Asp.Net page lifecycle runs through each and every one of those events in the Global.asax anyway.
HttpModule events run every single request and as long as you are not doing anything crazy in your logic, then you should be good to go. Even database lookups in the Application_BeginRequest method can be mitigated by proper caching.
And when in doubt, write some information out to the Trace in order to find out exactly how long your routine takes. I think you will find that compared to your most expensive operations (database lookups), the time will be negligible.
There are very robust solutions out there if you are going to use it more frequently and that imitate Apache module mod_rewrite, i like this one because i have used it and it gave me no problem:
or:
http://www.managedfusion.com/products/url-rewriter/
You can read more options in this post:
http://stackoverflow.com/questions/2262/aspnet-url-rewriting
As Josh says the essential article is: http://weblogs.asp.net/scottgu/archive/2007/02/26/tip-trick-url-rewriting-with-asp-net.aspx
That article is a bit old... and there are better approaches in the .NET framework now. What's funny is I used to do exactly what you are doing (hijacking the Error handler).
http://www.singingeels.com/Blogs/Nullable/2007/09/14/URL_ReWriting_The_Right_Way_Its_Easy.aspx
That's what I think you want to be doing now. Oh, and about performance... that adds about 0.00001 seconds to your page time.
Just a note to others who may be having trouble. Make sure to have
<modules runAllManagedModulesForAllRequests="true">
in your web.config