Question

Possible Duplicate:
Any way to add HttpHandler programatically in .NET?

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.

Was it helpful?

Solution

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.

OTHER TIPS

You can't modify the handlers but you can add a route to your hander programatically following these steps:

  1. If its a WebForms app then ensure your webapp has the UrlRouting configuration in the web.config (yip breaks part of your initial requirement to have minimal changes to web.config) as explained by MS here: Use Routing with Web Forms
  2. Implement the IRouteHandler interface on your handler and return the hander as the result in its methods (see first example below)
  3. Register your route (see second example below)

Implement IRouteHandler

public class myHandler : IHttpHandler, IRouteHandler
{
    public bool IsReusable
    {
        get { return true; }
    }

    public void ProcessRequest(HttpContext context)
    {
        // your processing here
    }

    public IHttpHandler GetHttpHandler(RequestContext requestContext)
    {
        return this;
    }
}

Register Route:

//from global.asax.cs
protected void Application_Start(object sender, EventArgs e)
{
    RouteTable.Routes.Add(new Route
    (
        "myHander.axd",
        new myHandler()
    ));
}

There you have it. A hander registered though code. :)

When creating a control, I usually override the GetDesignTimeHtml function in my ControlDesigner class. From that function I check if the HttpHandler is registered, and if not, I register it. Here is the code I use to register the new HttpHandler:

    private void RegisterHttpHandler()
    {

        IWebApplication webApplication =
            (IWebApplication)this.GetService(typeof(IWebApplication));

        if (webApplication != null)
        {
            Configuration configuration = webApplication.OpenWebConfiguration(false);
            if (configuration != null)
            {
                HttpHandlersSection section =
                    (HttpHandlersSection)configuration.GetSection(
                    "system.web/httpHandlers");
                if (section == null)
                {
                    section = new HttpHandlersSection();
                    ConfigurationSectionGroup group =
                        configuration.GetSectionGroup("system.web");
                    if (group == null)
                    {
                        configuration.SectionGroups.Add("system.web",
                            new ConfigurationSectionGroup());
                    }
                    group.Sections.Add("httpHandlers", section);
                }
                section.Handlers.Add(Action);
                configuration.Save(ConfigurationSaveMode.Minimal);
            }
        }

    }

The Action property is a static HttpHandlerAction

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top