Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/36.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在使用自定义服务器控件时自动修改web.config?_C#_Asp.net_Web Config_Custom Server Controls - Fatal编程技术网

C# 如何在使用自定义服务器控件时自动修改web.config?

C# 如何在使用自定义服务器控件时自动修改web.config?,c#,asp.net,web-config,custom-server-controls,C#,Asp.net,Web Config,Custom Server Controls,我正在尝试使用VS2008在C#中创建一个自定义服务器控件。我使用的这个自定义控件实际上需要我修改web.config文件,以便在将该控件添加到客户端页面时添加HttpHandler 我的问题很简单: 需要向我的自定义控件代码添加什么,以便它在web.config中注册所需的HttpHandler信息?一些本机控件已经这样做了。例如,ajaxtoolkit将修改web.config。如何对控件执行类似操作?查看线程,该线程描述如何在运行时完成类似任务。希望这能有所帮助。如果Visual Stud

我正在尝试使用VS2008在C#中创建一个自定义服务器控件。我使用的这个自定义控件实际上需要我修改
web.config
文件,以便在将该控件添加到客户端页面时添加
HttpHandler

我的问题很简单:


需要向我的自定义控件代码添加什么,以便它在
web.config
中注册所需的
HttpHandler
信息?一些本机控件已经这样做了。例如,
ajaxtoolkit
将修改web.config。如何对控件执行类似操作?

查看线程,该线程描述如何在运行时完成类似任务。希望这能有所帮助。

如果Visual Studio工具箱中有一项,那么当开发人员将控件拖放到网页上时,您可以执行这样的操作。您需要使用
System.ComponentModel.DesignerAttribute
装饰控件,以将其引用到
System.Web.UI.Design.ControlDesigner
的子类(由您创建)。在这个类中,您可以覆盖
初始化
,在这个类中,您可以通过
System.Web.UI.Design.IWebApplication
获得配置,如下所示:

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
}

我已经写了下面的代码,但它不起作用。如果(service!=null){var configuration=service.OpenWebConfiguration(false);如果(configuration!=null){var section=configuration.GetSection(“system.web/httpModules”),请检查我是否有任何错误作为HttpHandlersSection;HttpModuleAction a=新建HttpModuleAction(“工具”,“抄送,工具”);节。添加(a);添加节后,需要执行
配置。保存()
我添加了节配置。保存(),但它仍然不起作用,请帮帮我。@KrishnaYadav我对示例进行了更多的充实,添加了HttpHandleration代码。您可能需要在
else
块中添加一些代码,以确保不会出现这种情况。