Here's the scenario:
I have an HttpHandler that I'd like to run for specific URLs, but those UR开发者_如何学GoLs don't have consistent extensions, so I can't map the handler using httpHandlers in web.config.
Instead, I've got a custom HttpModule in which I subscribe to PostAuthenticateRequest, check some conditions, and assign my custom handler using HttpContext.RemapHandler() if the current URL qualifies.
That's all working great, but...
To improve performance, I'd like to do my URL checking and handler assignment only if a handler isn't already assigned. I won't bore you with why my handler only applies when no other handler is assigned, but it's always the case.
The real problem here is that one can only test if a handler is already assigned on PostMapRequestHandler, and by that time, it's too late to use RemapHandler() because an InvalidOperationException will get thrown (see http://msdn.microsoft.com/en-us/library/system.web.httpcontext.remaphandler.aspx)
What I thought may work at that point (but doesn't) is HttpContext.Handler = x; instead of HttpContext.RemapHandler(x);
When set this way, HttpContext.CurrentHandler gets updated and all appears glorious, but the custom handler's ProcessRequest() never gets called.
Thank you in advance for your thoughts / comments / insight.
Have you seen this? You could use routing to get your non same ending extensions to the same handler.
See http://learn.iis.net/page.aspx/496/iis-url-rewriting-and-aspnet-routing/
UPDATED
I would suggest that if you are having problem setting handlers you could try handling the PostResolveRequestCache
event. This is the point that the Url Routing Module plugs in to select a handler by calling context.RemapHandler
. Possibly you could even override the PostResolveRequestCache
method of UrlRoutingModule
and call base.PostResolveRequestCache(context)
then set your handler if required.
Alternatively you could look at always mapping your handler at the bottom of the stack in web.config with a catch all. Then pass-through requests that are not interesting or unmap the handler (not sure of the unmap will work) if not required.
Hope this help. Please let us know how you go.
精彩评论