简体   繁体   中英

Adding handler in <system.webServer>

I am new to Asp.Net and I want to know if it is possible to add a handler tag in < system.web > and < system.webServer> in web.config file dynamically during design time so that I can add a handler for image.

I need this information because I'm creating an image in ac# project. I'll compile it and generate a dll for the project. By using this dll, I want to use the image in a different project. So, when I add reference to the dll in any of my new project, I would like to add the handler during design time instead of changing it manually in web.config file.

You could use PreApplicationStartMethod with assembly attribute to register dynamically without adding to web.config. Add this code to your external library and it should work.

[assembly: System.Web.PreApplicationStartMethod(typeof(PreApplicationConfiguration), "Configure")]
namespace MyExtrarnalLibrary
{

    public   class PreApplicationConfiguration  {

        public static void Configure()
        { 
            //to register handler - handler must implement IRouteHandler
            RouteTable.Routes.Add(new Route("myspecialurl.asmx",new MyHttpHandler()));
            // if you want to register httpmodule , you can do this.
            Microsoft.Web.Infrastructure.DynamicModuleHelper.DynamicModuleUtility.RegisterModule(typeof(MyHttpModule));    
        }

    } 
}

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