【发布时间】:2021-04-20 08:04:36
【问题描述】:
我在初始应用程序设置期间使用以下代码注册 httpHandler。
class SomeHandler: IHttpAsyncHandler {
public void Register() {
using (ServerManager serverManager = new ServerManager()) {
string SITE_NAME = HostingEnvironment.ApplicationHost.GetSiteName();
string APP_PATH = HostingEnvironment.ApplicationHost.GetVirtualPath();
string SERVICE_CLASS = GetType().FullName.ToString();
string HANDLER_NAME = GetType().Name;
ConfigurationElementCollection CEC = serverManager
.GetWebConfiguration(SITE_NAME, APP_PATH)
.GetSection("system.webServer/handlers")
.GetCollection();
ConfigurationElement ele = null;
foreach (ConfigurationElement ele1 in CEC) {
if (ele1.Attributes["name"].Value.ToString() == HANDLER_NAME) {
ele = ele1;
}
}
if (ele == null) {
ele = CEC.CreateElement("add");
ele["name"] = HANDLER_NAME;
...
CEC.Add(ele);
serverManager.CommitChanges();
}
}
}
...
}
问题
它正确添加了处理程序,并且处理程序也可以正常工作。但是有一个问题。 最初我的 web.config 看起来像这样
<configuration>
<appSettings />
<system.webServer>
<handlers>
</handlers>
...
</system.webServer>
<system.web>
...
</system.web>
</configuration>
上面的代码想要的效果应该如下
<configuration>
<appSettings />
<system.webServer>
<handlers>
<add name="SomeHandler" path="..." verb="..." type="NameSpace.SomeHandler" />
</handlers>
...
</system.webServer>
<system.web>
...
</system.web>
</configuration>
但是我得到了关注
注意清除处理程序并添加所有从 IIS 服务器(其中 72 个)继承的处理程序,然后在最后添加我的处理程序。
问题
是否有可能以某种方式阻止 CommitChanges() 方法添加 clear 然后将所有处理程序添加到本地 web.config?
【问题讨论】:
标签: c# asp.net asp.net-4.5