在本地运行Web应用程序的Web.Debug.config与Web.Release.config [英] Web.Debug.config vs. Web.Release.config running web app in localhost
问题描述
我有一个Web.config文件,其中包含一些条目,但是要根据我在本地执行Web应用程序时是处于调试模式还是发布模式,我想采用不同的appSettings.
I have a Web.config file with some entries in there but depending on whether I am in Debug or Release mode when I execute my Web Application locally, I want to take different appSettings.
例如,假设我在Web.Debug.config appSettings中具有以下条目.
For instance, let's say that I have the following entry in my Web.Debug.config appSettings.
<add key="MyServiceUrl" value="http://my-test-site:8080/" />
我的Web.Release.config中也有这个
And also I have this in my Web.Release.config:
<add key="MyServiceUrl" value="http://my-prod-site:80/" />
我应该如何配置Web.Config,Web.Debug.Config和Web.Release.Config,以便根据我在本地运行应用程序的模式(调试-任何CPU 与发布-任何CPU ),需要正确的CPU吗?
How should I configure my Web.Config, Web.Debug.Config and Web.Release.Config so depending on the mode I run my application locally (Debug - Any CPU vs. Release - Any CPU), it takes the right one?
现在,无论我在Visual Studio中选择调试"还是发布",它唯一需要的一对键和值就是来自主Web.Config的键和值.
Right now, the only pair of key and value that it takes is the one from the main Web.Config regardless I select Debug or Release in Visual Studio.
如何配置该行为?
编辑
这就是我定义Web.config的方式
This is how I have defined Web.config
<appSettings>
<add key="webpages:Enabled" value="false" />
<add key="ClientValidationEnabled" value="true" />
<add key="UnobtrusiveJavaScriptEnabled" value="true" />
</appSettings>
这就是我定义Web.Debug.config
This is how I have defined Web.Debug.config
<appSettings>
<add key="MyServiceUrl" value="http://my-test-site:8080/" xdt:Transform="SetAttributes" xdt:Locator="Match(key)"/>
</appSettings>
这就是我定义Web.Release.config
This is how I have defined Web.Release.config
<appSettings>
<add key="MyServiceUrl" value="http://my-prod-site:8080/" xdt:Transform="SetAttributes" xdt:Locator="Match(key)" />
</appSettings>
最后,在我的代码中,我有以下方法:
Finally, in my code, I have the following method:
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");
which I call it like this: string setting = GetAppSetting("MyServiceUrl");
但是,如果未在主Web.config中定义,则为null
However, it is null if it is not defined in the main Web.config
推荐答案
在web.Release.config中尝试一下,它应该可以工作:
In the web.Release.config try this, it should work:
<appSettings>
<add key="MyServiceUrl" value="http://my-prod-site:8080/" xdt:Transform="Insert" />
</appSettings>
阅读此内容: Web.config Web应用程序项目部署的转换语法
这篇关于在本地运行Web应用程序的Web.Debug.config与Web.Release.config的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!