C# WebConfig合并应用程序设置

C# WebConfig合并应用程序设置,c#,asp.net,asp.net-web-api,asp.net-web-api2,C#,Asp.net,Asp.net Web Api,Asp.net Web Api2,情况是这样的,我想在我的Web.config中有一个键,该键在我的Web.Debug.config和我的Web.Release.config中更改,但我不确定如何实现这一点 我有我的Web.config: <appSettings> <add key="webpages:Version" value="3.0.0.0"/> <add key="webpages:Enabled" value="false"/> <add key

情况是这样的,我想在我的
Web.config
中有一个
键,该键在我的
Web.Debug.config
和我的
Web.Release.config
中更改,但我不确定如何实现这一点

我有我的
Web.config

  <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>
这:


第一个选项是正确的。完整的Web.Debug.config应如下所示:

<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
  <appSettings>
    <add key="RunningHost" value="http://localhost:55169/" xdt:Transform="Insert" />
  </appSettings>
</configuration>


您可以右键单击解决方案资源管理器中的Web.Debug.config并选择Preview Transform以查看转换后的输出。

因此我找到了这个问题的答案

这是因为当我调试web项目时,它没有编译一个新的配置,将
web.debug.config
web.config
结合起来

但是,如果您发布它以供发布,那么它将结合
web.release.config
web.config

因此,简单的修复方法是将
RunningHost
放在
web.config
中。然后让释放覆盖它

<appSettings xdt:Transform="Insert">
  <add key="RunningHost" value="http://localhost:55169/" />
</appSettings>
<appSettings xdt:Transform="Replace">
  <add key="RunningHost" value="http://localhost:55169/" />
</appSettings>
var appSetting = WebConfigurationManager.AppSettings["RunningHost"];
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
  <appSettings>
    <add key="RunningHost" value="http://localhost:55169/" xdt:Transform="Insert" />
  </appSettings>
</configuration>