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.
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.
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.
JSON files, specifically the `appsettings. json`, have replaced the XML based `web. config` file.
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.
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
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With