Web.Debug.config vs. Web.Release.config running web app in localhost

此生再无相见时 提交于 2020-01-05 07:17:48

问题


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.

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/" />

And also I have this in my Web.Release.config:

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

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?

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.

How can I configure that behavior?

EDIT

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>

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>

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];
        }

which I call it like this: string setting = GetAppSetting("MyServiceUrl");

However, it is null if it is not defined in the main Web.config


回答1:


In the web.Release.config try this, it should work:

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

Read this: Web.config Transformation Syntax for Web Application Project Deployment



来源:https://stackoverflow.com/questions/40921566/web-debug-config-vs-web-release-config-running-web-app-in-localhost

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!