Web.Debug.config与在localhost中运行Web应用程序的Web.Release.config

时间:2016-12-01 22:28:58

标签: c# asp.net web-config web-config-transform

我有一个Web.config文件,其中包含一些条目,但是当我执行我的Web应用程序本地时,根据我是处于调试模式还是发布模式,我想采用不同的appSettings。

例如,让我说我的Web.Debug.config appSettings中有以下条目。

<add key="MyServiceUrl" value="http://my-test-site:8080/" />

而且我的Web中也有这个.Release.config:

<add key="MyServiceUrl" value="http://my-prod-site:80/" />

我应该如何配置我的Web.Config,Web.Debug.Config和Web.Release.Config,这取决于我在本地运行我的应用程序的模式(调试 - 任何CPU 发布 - 任何CPU ),它是正确的吗?

现在,唯一的一对键和值是来自主Web.Config的那一个,无论我在Visual Studio中选择Debug还是Release。

如何配置该行为?

修改

这就是我定义Web.config

的方法
<appSettings>
    <add key="webpages:Enabled" value="false" />
    <add key="ClientValidationEnabled" value="true" />
    <add key="UnobtrusiveJavaScriptEnabled" value="true" />
</appSettings>

这就是我定义Web.Debug.config

的方法
<appSettings>
     <add key="MyServiceUrl" value="http://my-test-site:8080/" xdt:Transform="SetAttributes" xdt:Locator="Match(key)"/>
</appSettings>

这就是我定义Web.Release.config

的方法
<appSettings>
     <add key="MyServiceUrl" value="http://my-prod-site:8080/" xdt:Transform="SetAttributes" xdt:Locator="Match(key)" />
</appSettings>

最后,在我的代码中,我有以下方法:

public static string GetAppSetting(string settingKey)
        {
            if (string.IsNullOrEmpty(settingKey))
                throw new System.ArgumentException("Cannot fetch a setting for a null/empty setting key.");
            return ConfigurationManager.AppSettings[settingKey];
        }

我称之为: string setting = GetAppSetting(&#34; MyServiceUrl&#34;);

但是,如果未在主Web.config

中定义它,则为null

1 个答案:

答案 0 :(得分:1)

在web.Release.config中尝试这个,它应该可以工作:

<appSettings>
 <add key="MyServiceUrl" value="http://my-prod-site:8080/" xdt:Transform="Insert" />
</appSettings>

阅读本文:Web.config Transformation Syntax for Web Application Project Deployment

相关问题