Config Error: This configuration section cannot be used at this path

Config Error: This configuration section cannot be used at this path

技术背景

在使用 IIS(Internet Information Services)服务器时,可能会遇到 “Config Error: This configuration section cannot be used at this path” 错误。此错误通常表示配置部分在当前路径被锁定,无法使用,需要对相关配置文件进行修改以解决问题。

实现步骤

方法一:启用 Windows 功能

  1. 点击 “开始按钮”,在搜索框中输入 “Turn windows features on or off” 并打开。
  2. 在功能窗口中,依次点击 “Internet Information Services”、“World Wide Web Services”、“Application Development Features”。
  3. 勾选(启用)相关功能,可除 CGI 外全部勾选。

方法二:使用 IIS Manager 编辑设置

  1. 打开 IIS Manager,从根目录使用 “Feature Delegation” 选项。
  2. 控制每个功能的权限,若尝试使用设置为只读的功能,会出现 overrideMode="Deny" 错误,将其改为可读写。

方法三:修改 applicationHost.config 文件

  1. 浏览到 “C:\Windows\System32\inetsrv\config”(需要管理员权限),打开 applicationHost.config 文件。在 IISExpress 和 Visual Studio 2015 中,该文件存储在 $(solutionDir).vs\config\applicationhost.config
  2. 找到错误消息页面 “config source” 部分显示的部分,常见为 “modules” 或 “handlers”。
  3. overrideModeDefault 属性改为 Allow,例如:
1
<section name="modules" allowDefinition="MachineToApplication" overrideModeDefault="Allow" />
  1. 保存文件后,在浏览器中重新加载页面。

方法四:解锁处理程序

使用以下 cmd 命令解锁处理程序:

1
%windir%\system32\inetsrv\appcmd.exe unlock config -section:system.webServer/handlers

方法五:解锁认证配置

在提升权限的命令提示符中运行以下命令:

1
2
%windir%/system32/inetsrv/appcmd unlock config /section:anonymousAuthentication
%windir%/system32/inetsrv/appcmd unlock config /section:windowsAuthentication

方法六:使用 IIS 配置编辑器解锁设置

  1. 打开 IIS Manager,选择服务器。
  2. 在主面板中打开 “Configuration Editor”。
  3. 在 “Sections” 下拉列表中,选择要解锁的部分,如 “system.webServer > defaultPath”。
  4. 点击右侧面板的 “Unlock Attribute”。
  5. 对需要解锁的其他设置重复上述步骤。
  6. 可选:重启 IIS。

方法七:更改应用程序设置

  1. 打开 IIS,从根目录选择 “Feature Delegation”,然后选择 “Application Settings”。
  2. 在右侧侧边栏选择 “Read/Write”。

方法八:启用特定功能(以 Windows Server 2012 为例)

  1. 启用 “ASP.NET 4.5” 功能。
  2. 参考相关解决方案(如 ken 的答案)。

方法九:检查文件权限

进入出现拒绝错误的应用程序路径,右键点击,选择 “Properties -> Security tab”,更改权限并勾选读写复选框。

方法十:使用 PowerShell 启用功能(Windows Server 2012 +)

1
2
3
4
5
6
7
Install-WindowsFeature NET-Framework-Core
Install-WindowsFeature Web-Server -IncludeAllSubFeature
Install-WindowsFeature NET-Framework-Features -IncludeAllSubFeature
Install-WindowsFeature NET-Framework-45-ASPNET -IncludeAllSubFeature
Install-WindowsFeature Application-Server -IncludeAllSubFeature
Install-WindowsFeature MSMQ -IncludeAllSubFeature
Install-WindowsFeature WAS -IncludeAllSubFeature

方法十一:检查配置文件层次结构

按照以下顺序检查应用配置设置:

  1. C:\windows\system32\inetsrv\config 中的 ApplicationHost.config,将 overrideModeDefault 属性改为 Allow
  2. 应用程序目录中的 ApplicationName.configweb.config
  3. 根目录中的 web.config
  4. 特定网站的 web.config
  5. 根 Web(服务器配置)的 web.config
  6. 机器的 machine.config(位于 systemroot\MicrosoftNET\Framework\versionNumber\CONFIG\Machine.config)。

方法十二:其他方法

  • 若在 IIS 8 中,可将服务器更改为 IIS Express,在调试 -> 属性中,在 Web 选项中从下拉列表选择 IIS Express,然后重新构建解决方案。
  • 在应用程序级别(Web.Config),可移除 trust level 标签:
1
<trust level="Full"/>
  • 执行 aspnet_regiis -i 命令。
  • 检查 C:\inetpub\history 中的备份文件,将正确的 applicationhost.config 文件替换当前版本(先备份)。
  • 在注册表中添加以下键:
1
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\services\ASP.NET_64\Performance

核心代码

修改 applicationHost.config 文件示例

1
2
3
4
<section name="windowsAuthentication" overrideModeDefault="Allow" />
<section name="anonymousAuthentication" overrideModeDefault="Allow" />
<add name="WindowsAuthenticationModule" lockItem="false" />
<add name="AnonymousAuthenticationModule" lockItem="false" />

PowerShell 启用功能代码

1
2
3
4
5
6
7
Install-WindowsFeature NET-Framework-Core
Install-WindowsFeature Web-Server -IncludeAllSubFeature
Install-WindowsFeature NET-Framework-Features -IncludeAllSubFeature
Install-WindowsFeature NET-Framework-45-ASPNET -IncludeAllSubFeature
Install-WindowsFeature Application-Server -IncludeAllSubFeature
Install-WindowsFeature MSMQ -IncludeAllSubFeature
Install-WindowsFeature WAS -IncludeAllSubFeature

解锁处理程序和认证配置的 cmd 命令

1
2
3
%windir%\system32\inetsrv\appcmd.exe unlock config -section:system.webServer/handlers
%windir%/system32/inetsrv/appcmd unlock config /section:anonymousAuthentication
%windir%/system32/inetsrv/appcmd unlock config /section:windowsAuthentication

最佳实践

  • 在修改配置文件前,务必备份原始文件,以防出现问题。
  • 按照配置文件层次结构依次检查和修改,确保不遗漏可能的问题。
  • 若不确定如何修改某个配置项,可参考官方文档或相关技术论坛。

常见问题

32 位编辑器无法保存配置文件

在 64 位系统上,32 位编辑器(如 32 位 Notepad++)可能无法正确保存 applicationHost.config 文件。可使用 64 位的记事本进行编辑。

更改配置后仍有错误

可能是由于部分配置未正确修改或存在其他依赖项问题。可检查事件查看器获取更多错误信息,或参考官方文档进一步排查。


Config Error: This configuration section cannot be used at this path
https://119291.xyz/posts/config-error-this-configuration-section-cannot-be-used-at-this-path/
作者
ww
发布于
2025年5月22日
许可协议