You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

在IIS的applicationHost.config中配置URL Rewrite服务器变量失败求助

IIS配置问题:通过ServerManager修改applicationHost.config中Rewrite允许的服务器变量失败

问题描述

我在使用Application Request Routing(ARR)搭配URL Rewrite时,需要在安装程序中配置服务器变量HTTP_SEC_WEBSOCKET_EXTENSION:

  • web.config:可通过ServerManager类正常配置
  • applicationHost.config:配置出错

参考微软IIS官方文档编写C#代码时,调用GetApplicationHostConfiguration()获取system.webServer/rewrite/allowedServerVariables/add节点失败,错误提示程序尝试读取web.config,且该配置节缺少声明。我不解为何使用GetApplicationHostConfiguration()却读取web.config,需要解决方法。

代码示例

Configuration applicationHostConfig = serverManager.GetApplicationHostConfiguration();
// 此处路径无法正常工作,路径是手动添加变量后从applicationHost.config中获取的
ConfigurationSection section = applicationHostConfig.GetSection("system.webServer/rewrite/allowedServerVariables/add", "MyWeb");
ConfigurationAttribute enabled = section.GetAttribute("name");
enabled.Value = "HTTP_SEC_WEBSOCKET_EXTENSION";

// 微软示例中的部分代码可正常运行
ConfigurationSection section = applicationHostConfig.GetSection("system.webServer/defaultDocument", "MyWeb");
ConfigurationAttribute enabled = section.GetAttribute("enabled");
enabled.Value = false;
serverManager.CommitChanges();

错误信息

System.Runtime.InteropServices.COMException (0x80070490): Filename: \\?\C:\WWW\MyWeb\web.config
Error: The configuration section 'system.webServer/rewrite/allowedServerVariables/add' 
cannot be read because it is missing a section declaration

解决方法

问题原因

allowedServerVariables是全局级别的配置节,只能在applicationHost.config的根级别配置,不能指定站点("MyWeb")作为第二个参数。当你传入站点名称时,IIS会尝试去该站点的web.config中查找这个配置节,但web.config里并没有注册这个节,因此报错。

修改后的代码

using (ServerManager serverManager = new ServerManager())
{
    Configuration applicationHostConfig = serverManager.GetApplicationHostConfiguration();
    // 获取allowedServerVariables根节,不指定站点
    ConfigurationSection allowedServerVariablesSection = applicationHostConfig.GetSection("system.webServer/rewrite/allowedServerVariables");
    ConfigurationElementCollection allowedServerVariablesCollection = allowedServerVariablesSection.GetCollection();

    // 检查变量是否已存在,避免重复添加
    bool variableExists = allowedServerVariablesCollection.Any(item => item.GetAttributeValue("name").ToString() == "HTTP_SEC_WEBSOCKET_EXTENSION");
    if (!variableExists)
    {
        ConfigurationElement addElement = allowedServerVariablesCollection.CreateElement("add");
        addElement.SetAttributeValue("name", "HTTP_SEC_WEBSOCKET_EXTENSION");
        allowedServerVariablesCollection.Add(addElement);
    }

    // 原有的defaultDocument配置可保留(如果需要)
    ConfigurationSection defaultDocumentSection = applicationHostConfig.GetSection("system.webServer/defaultDocument", "MyWeb");
    defaultDocumentSection.SetAttributeValue("enabled", false);

    serverManager.CommitChanges();
}

额外说明

  • allowedServerVariables是服务器级别的白名单配置,必须在applicationHost.config全局范围操作,不能绑定到单个站点。
  • 操作时需确保程序拥有管理员权限,否则无法修改applicationHost.config。
  • 若要针对单个站点修改服务器变量的具体值(而非添加到白名单),才需要操作web.config,此时可使用serverManager.GetWebConfiguration("MyWeb")获取站点配置。

内容的提问来源于stack exchange,提问作者Abedron

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.22 11:50:09