views:

360

answers:

2

Is there a way I can dynamically register an IHttpHandler in C# code, instead of having to manually add it to the system.web/httpHandlers section in the web.config.

This may sound crazy, but I have good reason for doing this. I'm building a WidgetLibrary that a website owner can use just by dropping a .dll file into their bin directory, and want to support this with minimal configuration to the web.config.

+1  A: 

Here's an article about dynamically altering a web.config file through code

http://highoncoding.com/Articles/119_Writing_in_Web_config_file_dynamically.aspx

hunter
I thought about modifying the web.config in code, but wouldn't this cause the web AppDomain to reset?
Sunday Ironfoot
certainly there are side effects to modifying the web.config but if you're only doing this once (dynamically at app start?) then it should make a difference.
hunter
If you're doing a custom library then adding an element to the web.config is pretty standard. I like the idea of making it as easy as possible but you might be wasting effort.
hunter
+4  A: 

I don't believe it's possible to modify the registered HttpHandlers once the AppDomain is running because the available handlers is read directly from the web.config file then cached in a private data structure.

If you knew upfront which extensions you wanted to allow, you could do is map these extensions to a single HttpHandlerFactory and then return a handler of your choice (by using dynamic assembly loading and reflection). For example:

<add path="*.ch1,*.ch2,*.ch3" verb="*" 
    type="MyHandlers.MyHandlerFactory, MyHandlers" />

Modifying the web.config at runtime would cause the AppDomain to restart.

Kev