views:

317

answers:

3

In ASP.NET is there any way to programatically resolve the path to a loaded HttpHandler as it is defined in the Web.config? (i.e. SomeModule.axd)?

A: 

From the current http context use the path property of the Request object

Kevin
+3  A: 

If I understand the question correctly, you want to fetch the path from web.config right?

If so, what you are probably looking for is something like this:

string p = null;
System.Web.Configuration.HttpHandlersSection httpHandlersSection =
    (System.Web.Configuration.HttpHandlersSection)
        System.Configuration.ConfigurationManager.GetSection("system.web/httpHandlers");

foreach (System.Web.Configuration.HttpHandlerAction handler in httpHandlersSection.Handlers)
{
    if(handler.Type == "myType")
    {
        p = handler.Path;
        break;
    }
}

The trick here is the if statement. Handlers in web.config don't have friendly "names" you can use as a key. All they have are types (which can be ugly strings), paths, and the verb. To locate the specific handler you are interested in you might have to search within the handler's type or path for a known substring that identifies the specific handler you are interested in finding.

Stephen M. Redd
That's a pretty decent solution albeit indirect (by .NET standards). It would be very nice if there were some sort of framework standard service locator that could be mapped to HttpHandlers for easy interfacing from separate modules. Something worth considering I'd say!
Nathan Taylor
Generally speaking, it is unusual for user code to have a need to examine the internals of the application's configuration at that level. Plus there are handlers that are built-in and others that are registered via machine level configuration. Then there are HttpHandlerFactories that dynamically register handlers on demand. Between the dynamic nature of handlers and the potential security considerations I can see why there isn't much in the way of direct access to these object.
Stephen M. Redd
For those that had the same headache I did, remember, the "system.web/httpHandlers" IS case sensitive. That means the "H" in Handlers is a capitol.
International Spectrum
A: 

If you're interested in the path to the handler processing the current request:

string path = HttpContext.Current.Handler.GetType().Assembly.CodeBase;

If you happened to know where to find a collection of the other handler instances, you could use the same approach to get their paths as well.

Andrew Arnott
I'm actually trying to resolve the "virtual path" to the handler, not the location of the DLL itself. I.e. if the "MyHandler" type is registered at "MyHandler.axd", I want to resolve "MyHandler.axd".
Nathan Taylor