locked
How to get HttpHandler class for a given url? Please help! RRS feed

  • Question

  • User416536934 posted

    Hello there,

    I was wondering if there is any way to get the HttpHandler for a given path.

    Something like the following:

     

    var url = "~/MyFolder/MyFile.axd";

    IHttpHandler = MagicObject.GetHandler(url);

     

    Does that "MagicObject" exist somewhere in the .NET framework? :)

    Thank you, bye.

    Friday, November 4, 2011 10:01 AM

Answers

  • User1622957740 posted

    There's no built-in tool to retrieve Handlers defined in ASP.NET unforatunately - you can get Modules, but not handlers which is kind of silly.

    In fact, there's not even an easy way to even get a list of all Http handlers active at a given site or application. The process can be complicated because there are actually two sections that might apply (system.web/httpHandlers or system.webServer/handlers in IIS 7+).

    For non-integrated mode access it's possible to loop through declared handlers and you can then compare against your file spec:

                Configuration cfg = WebConfigurationManager.OpenWebConfiguration("/westwindwebtoolkitweb");            
                ConfigurationSection cfgSection = cfg.GetSection("system.web/handlers");
                var xml = cfgSection.ToString();
                HttpHandlersSection handlers = (HttpHandlersSection)cfgSection;
    
                foreach(HttpHandlerAction handler in handlers.Handlers)
                {
                    Response.Write( handler.Path + " - " + handler.Type + " - " + handler.Verb + "<hr/>");
                }

    Unfortunately on IIS 7 the system.web/handlers section is locked so you don't get the same type of access to that section. For IIS 7 one possibility is to loop through the defined handlers manually:

                Configuration cfg = WebConfigurationManager.OpenWebConfiguration("/westwindwebtoolkitweb");            
                ConfigurationSection cfgSection = cfg.GetSection("system.web/httpHandlers");
                var xml = cfgSection.ToString();
                HttpHandlersSection handlers = (HttpHandlersSection)cfgSection;
    
                foreach(HttpHandlerAction handler in handlers.Handlers)
                {
                    Response.Write( handler.Path + " - " + handler.Type + " - " + handler.Verb + "<hr/>");
                }

    But keep in mind that this will only give you the handlers defined in the current app, not the entire hierarchy up the chain like the first example.

    Not exactly what you're looking for but maybe this points you in the right direction...

    +++ Rick ---

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, November 10, 2011 8:23 PM

All replies

  • User1622957740 posted

    There's no built-in tool to retrieve Handlers defined in ASP.NET unforatunately - you can get Modules, but not handlers which is kind of silly.

    In fact, there's not even an easy way to even get a list of all Http handlers active at a given site or application. The process can be complicated because there are actually two sections that might apply (system.web/httpHandlers or system.webServer/handlers in IIS 7+).

    For non-integrated mode access it's possible to loop through declared handlers and you can then compare against your file spec:

                Configuration cfg = WebConfigurationManager.OpenWebConfiguration("/westwindwebtoolkitweb");            
                ConfigurationSection cfgSection = cfg.GetSection("system.web/handlers");
                var xml = cfgSection.ToString();
                HttpHandlersSection handlers = (HttpHandlersSection)cfgSection;
    
                foreach(HttpHandlerAction handler in handlers.Handlers)
                {
                    Response.Write( handler.Path + " - " + handler.Type + " - " + handler.Verb + "<hr/>");
                }

    Unfortunately on IIS 7 the system.web/handlers section is locked so you don't get the same type of access to that section. For IIS 7 one possibility is to loop through the defined handlers manually:

                Configuration cfg = WebConfigurationManager.OpenWebConfiguration("/westwindwebtoolkitweb");            
                ConfigurationSection cfgSection = cfg.GetSection("system.web/httpHandlers");
                var xml = cfgSection.ToString();
                HttpHandlersSection handlers = (HttpHandlersSection)cfgSection;
    
                foreach(HttpHandlerAction handler in handlers.Handlers)
                {
                    Response.Write( handler.Path + " - " + handler.Type + " - " + handler.Verb + "<hr/>");
                }

    But keep in mind that this will only give you the handlers defined in the current app, not the entire hierarchy up the chain like the first example.

    Not exactly what you're looking for but maybe this points you in the right direction...

    +++ Rick ---

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, November 10, 2011 8:23 PM
  • User-1737239174 posted

    Hi,

    I was just having the same requirement. For thoses who are interested in the answer, you can use :

    BuildManager.CreateInstanceFromVirtualPath("~/pouet.aspx", typeof(Page))
    Wednesday, May 23, 2012 8:13 AM