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

如何在machine.config中添加appSetting配置项?

在machine.config中添加AppSetting的正确方式

web.config中的成功操作示例

在web.config中添加ServerType配置项的操作是正确的,前后配置片段如下:

修改前:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <appSettings>
    <add key="webpages:Version" value="3.0.0.0" />
    <add key="webpages:Enabled" value="false" />
    <add key="ClientValidationEnabled" value="true" />
    <add key="UnobtrusiveJavaScriptEnabled" value="true" />
  </appSettings>
  ...

修改后(新增ServerType):

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <appSettings>
    <add key="ServerType" value="I AM A WEB.CONFIG OVERRIDING MACHINE.CONFIG" />
    <add key="webpages:Version" value="3.0.0.0" />
    <add key="webpages:Enabled" value="false" />
    <add key="ClientValidationEnabled" value="true" />
    <add key="UnobtrusiveJavaScriptEnabled" value="true" />
  </appSettings>
  ...

machine.config中的错误尝试分析

machine.config修改前的核心片段:

<?xml version="1.0" encoding="UTF-8" ?>
<configuration>
    <configSections>
        <section name="appSettings" type="System.Configuration.AppSettingsSection, System.Configuration, Version=4.0.0.0, Culture=neutral, PublicKeyToken=blahblah" restartOnExternalChanges="false" requirePermission="false" />
        ...

尝试1:在
标签添加自定义属性

<?xml version="1.0" encoding="UTF-8" ?>
<configuration>
    <configSections>
        <section name="appSettings" type="System.Configuration.AppSettingsSection, System.Configuration, Version=4.0.0.0, Culture=neutral, PublicKeyToken=blahblah" restartOnExternalChanges="false" requirePermission="false" serverType="I AM MACHINE" />
        ...

结果:

无法显示页面,因为发生了内部服务器错误。

问题:<section>标签的属性是系统预定义的,不允许添加自定义属性,直接导致配置文件解析失败。

尝试2:在
标签内部嵌入节点

<?xml version="1.0" encoding="UTF-8" ?>
<configuration>
    <configSections>
        <section name="appSettings" type="System.Configuration.AppSettingsSection, System.Configuration, Version=4.0.0.0, Culture=neutral, PublicKeyToken=blahblah" restartOnExternalChanges="false" requirePermission="false">
            <add key="ServerType" value="I AM MACHINE" />
        </section>
        ...

结果:

运行时错误
描述:服务器上发生了应用程序错误。出于安全原因,当前应用程序的自定义错误设置阻止远程查看错误详细信息,但可在本地服务器浏览器查看。

问题:<configSections>下的<section>仅用于声明配置节的类型和行为规则,不能直接放置具体的配置项内容。

尝试3:在内部放置节点

<?xml version="1.0" encoding="UTF-8" ?>
<configuration>
    <configSections>
        <appSettings>
        <add key="ServerType" value="I AM MACHINE" />
    </appSettings>
        <section name="appSettings" type="System.Configuration.AppSettingsSection, System.Configuration, Version=4.0.0.0, Culture=neutral, PublicKeyToken=blahblah" restartOnExternalChanges="false" requirePermission="false"/>
        ...

结果:

无法显示页面,因为发生了内部服务器错误。

问题:<configSections>节点的作用是定义配置节,只能包含<section>这类声明节点,实际的配置节内容必须放在<configSections>之外的<configuration>根节点下。

正确的machine.config配置方式

在machine.config中添加AppSetting,需要在**<configSections>节点之后**,独立添加<appSettings>节点(若原有配置已存在该节点,直接在其中追加即可),写法与web.config一致:

<?xml version="1.0" encoding="UTF-8" ?>
<configuration>
    <configSections>
        <section name="appSettings" type="System.Configuration.AppSettingsSection, System.Configuration, Version=4.0.0.0, Culture=neutral, PublicKeyToken=blahblah" restartOnExternalChanges="false" requirePermission="false" />
        ... <!-- 其他配置节声明 -->
    </configSections>

    <!-- 在此处放置appSettings配置节 -->
    <appSettings>
        <add key="ServerType" value="I AM MACHINE" />
    </appSettings>

    ... <!-- 其他系统配置节点,如system.web等 -->
</configuration>

补充说明

  • machine.config中的AppSetting优先级低于web.config,若web.config中存在同名的key,会覆盖machine.config中的值。
  • 修改machine.config后需要重启IIS或应用池,确保配置生效。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.21 04:06:26