views:

3492

answers:

5

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?

+2  A: 

Every 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.

Joe R
+1  A: 

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.

Josh
The logic of the rewritng is quite complicated, and from what I could gather about the HttpModule, it didn't look easier to do it that way. Plus, the global.asax is already in the project and I don't have to add any dll.
Lea Cohen
+3  A: 

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:

http://www.codeplex.com/IIRF

or:

http://urlrewriter.net/

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

netadictos
Could you please explain why those methods are better than the method I suggested?
Lea Cohen
You have an ISAPI where you can centralize a lot of stuff with many many options, for example, for domains as well as for urls. Look at these posts: http://www.codinghorror.com/blog/archives/000797.html or http://www.hanselman.com/blog/CommentView.aspx?guid=91da8c0a-537e-4bbf-abac-c6652dee7057
netadictos
+2  A: 

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.

Timothy Khouri
I'm still working with framework 2.0
Lea Cohen
OK, then go ahead with the Guthrie stuff... the performance hit is still incredibly negligible.
Timothy Khouri
A: 

Just a note to others who may be having trouble. Make sure to have

<modules runAllManagedModulesForAllRequests="true">

in your web.config

adinas