Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

appSettings in Web.config and Web.Release.config

Trying to get simple <appSettings> for dev vs. prod.

My Web.config:

<appSettings>
  <add key="hello" value="debug" />
</appSettings>

My Web.Release.config:

<appSettings>
  <add key="hello" value="prod" />
</appSettings>

(both under <configuration>)

When I have it in Debug mode, and run my MVC site, I can do a simple return Content(WebConfigurationManager.AppSettings["hello"]); in my HomeController.Index and it returns dev. If I switch the mode to Release it still returns dev. I'd like to simulate prod mode without actually publishing to prod.

like image 832
Ian Davis Avatar asked Feb 16 '17 01:02

Ian Davis


People also ask

What is appSettings in web config?

The <appSettings> element of a web. config file is a place to store connection strings, server names, file paths, and other miscellaneous settings needed by an application to perform work.

What is difference between web config and app config?

The web. config file is required for ASP.NET webpages. The app. config file is optional in an application and doesn't have to be used when writing desktop applications.

Does appSettings JSON replace web config?

JSON files, specifically the `appsettings. json`, have replaced the XML based `web. config` file.

What is web Debug config and web release config?

The Web. debug. config file stores changes that Microsoft Visual Studio applies to the Web. config file, when you compile the application in the Debug mode. Before you publish the application using release configuration, you need to remove the debug attribute from the <compilation> element in the Web.config file.


1 Answers

In the build-specific Web.config file, you have to tell it how to transform the base .config file. So to do what you ask, your Web.Release.config file should look like this:

<appSettings>
  <add key="hello" value="prod" xdt:Transform="SetAttributes" xdt:Locator="Match(key)" />
</appSettings>

In the above code the SetAttributes transform will change the attributes of any element that matches the key attribute containing the value hello.

like image 56
Nikolaj Dam Larsen Avatar answered Sep 28 '22 08:09

Nikolaj Dam Larsen