如何使用自定义服务器控件时自动修改的web.config? [英] How to Modify web.config automatically when using Custom Server Control?

查看:95
本文介绍了如何使用自定义服务器控件时自动修改的web.config?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在使用VS2008 C#创建一个自定义服务器控件。我使用的实际要求我修改的web.config 文件,以添加这个定制控件的的HttpHandler 时这个控制被添加到客户端的页

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.

我的问题很简单:

需要什么,以便它注册在 web.config中的要求的HttpHandler 信息?一些本地控件已经做到这一点。例如, Ajax工具包将修改web.config。我该怎么做类似的东西与我的控制?

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?

推荐答案

如果您在Visual Studio的工具箱中有一个项目时开发商拖放控制到Web页面,您可以执行这样的操作。您需要装饰与 System.ComponentModel.DesignerAttribute 你控制它交给一个子类(其中创建) System.Web.UI.Design的.ControlDesigner 。在这个类可以重写初始化,并在那里,你可以通过 System.Web.UI.Design.IWebApplication 是这样的:

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
}

这篇关于如何使用自定义服务器控件时自动修改的web.config?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆