我有一個Web.config文件,其中有一些條目,但取決於當我執行我的Web應用程序時處於調試模式還是發佈模式本地,我想採取不同的appSettings。Web.Debug.config與Web.Release.config在本地主機上運行web應用程序
例如,假設我的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),它採取正確的?
現在,無論我在Visual Studio中選擇調試還是發佈,唯一需要的鍵和值對都來自主Web.Config。
如何配置該行爲?
編輯
這是我如何定義的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];
}
我稱之爲它是這樣的: 串設定= GetAppSetting( 「MyServiceUrl」);
然而,這是空如果不是在主Web.config中
請將_
或使用:_xdt:Transform =「Insert」_ –