简体   繁体   中英

How to check if an IHttpHandler is loaded?

I have a global.asax and I would like to check in the Application_Start method if a IHttpHandler with a certain name is registered. How can I do this?

The handler can be declared in the apps own web.config or one of another virtual parent directories or the machine.config.

I'm not aware of a public method to do what you're asking for.

I think the code you want is either HttpContext.Current.ApplicationInstance.GetHandlerMapping(), .MapHttpHandler() or .MapIntegratedHttpHandler(), but they are private or internal methods. However, you should be able to use reflection to call them.

An alternative would be some trial-and-error type testing (try to request a target that should be routed to the expected handler, and see what happens in several place, such as in the MapRequestHandler event in an HttpModule -- but I think Application_Start is too early for that.

Read the web.config file using WebConfigurationManager.OpenWebConfiguration method. Due to ASP.NET Configuration File Hierarchy and Inheritance all these configuration sections which are added in machine.config are available to your web-app so you need to read local web.config.

Configuration config = WebConfigurationManager.OpenWebConfiguration("~/web.config");
HttpHandlersSection section = (HttpHandlersSection)config.GetSection("system.web/httpHandlers");

GridView1.DataSource = section.Handlers;
GridView1.DataBind();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM