Ian Davis
Ian Davis

Reputation: 19413

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.

Upvotes: 8

Views: 7360

Answers (2)

tchelidze
tchelidze

Reputation: 8308

Starting from .NET 4.7.1 feature called Configuration builder is supported which gives developer ability to load configuration not only from Web.Release.Cong but basically from any source. Read more about .NET Framework 4.7.1 ASP.NET and Configuration features

Upvotes: 1

Nikolaj Dam Larsen
Nikolaj Dam Larsen

Reputation: 5674

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.

Upvotes: 20

Related Questions