AppSettings in Web.config and Web.Release.config

Trying to get a simple <appSettings> for dev and prod.

My Web.config :

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

My Web.Release.config :

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

(both in <configuration> )

When I use 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 will return dev anyway. I would like to simulate prod mode without actually publishing to prod.

+7
source share
2 answers

In the assembly-specific Web.config file, you must tell it how to convert the base .config file. 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 hello value.

+16
source

Starting with .NET 4.7.1 , a feature called Configuration Configurator is supported , which allows the developer to download the configuration not only from Web.Release.Cong but mainly from any source. Learn more about the .NET Framework 4.7.1 ASP.NET and configuration features

0
source

Source: https://habr.com/ru/post/1015016/


All Articles