0

I am trying to create a Custom Server Control in C# using VS2008. I am using this custom control that actually requires me to modify the web.config file in order to add an HttpHandler when this control is added to the page of client.

My question is fairly simple:

What needs to be added to my custom control code so it registers the required HttpHandler information in the web.config? Some native controls already do this. For example, the AJAX Toolkit will modify the web.config. How can I do something similar with my control?

James Johnson
  • 45,496
  • 8
  • 73
  • 110
Krishna Yadav
  • 33
  • 1
  • 3
  • 8

2 Answers2

1

If you have an item in the toolbox of Visual Studio you can perform such an action when the developer drags and drops the control onto the web page. You need to decorate your control with System.ComponentModel.DesignerAttribute to refer it to a subclass (which you create) of System.Web.UI.Design.ControlDesigner. In this class you can override Initialize and in there you can get hold of the config via System.Web.UI.Design.IWebApplication something like this:

var service = this.GetService(typeof(System.Web.UI.Design.IWebApplication)) as IWebApplication;
if (service != null)
{
    var configuration = service.OpenWebConfiguration(false);
    if (configuration != null)
    {
        var section = configuration.GetSection("system.web/httpHandlers") as HttpHandlersSection;
        if (section != null)
        {
            var httpHandlerAction = new HttpHandlerAction("MyAwesomeHandler.axd", typeof(MyAwesomeHandler).AssemblyQualifiedName, "GET,HEAD", false);
            section.Handlers.Add(httpHandlerAction);                
            configuration.Save();
        }
        else 
        {
            // no system.web/httpHandlers section found... deal with it
        }
    }
    else 
    {
        // no web config found... 
    }
}
else 
{
    // Couldn't get IWebApplication service
}
Duncan Smart
  • 31,172
  • 10
  • 68
  • 70
  • i hav writen below code but it didnt work.Please chek if anything wrong i m doing if (service != null) { var configuration = service.OpenWebConfiguration(false); if (configuration != null) { var section = configuration.GetSection("system.web/httpModules") as HttpHandlersSection; HttpModuleAction a = new HttpModuleAction("Tools","CC,Tools") ;section.add(a); – Krishna Yadav Oct 04 '11 at 19:31
  • Once you've added the section you'll need to do a `configuration.Save()` – Duncan Smart Oct 06 '11 at 12:12
  • I have added the section configuration.Save(), But still it is not working ,Please help me. – Krishna Yadav Oct 10 '11 at 04:02
  • @KrishnaYadav I've fleshed out the example a bit more, adding the HttpHandlerAction code. You might want add some code in the `else` blocks to make sure you're not ending up there. – Duncan Smart Oct 10 '11 at 10:33
0

Review the Any way to add HttpHandler programmatically in .NET? thread, which descries how to accomplish a similar task at runtime. Hope this helps.

Community
  • 1
  • 1
Mikhail
  • 9,186
  • 4
  • 33
  • 49