Search code examples
asp.netvariablesdeploymentweb-configenvironment

ASP.NET how to set a variable in web.config during publising an app


I have an ASP.NET application and I would like to set a variable during publishing the application depending on the environment (like production, quality, etc....) I have created a variable in the web.config file in the <appSettings /> section inside <configuration /> section like this:

<add key="RunningEnvironment" value="dev"/>

I have two profiles when deploying dev and prod

How can I set the RunningEnvironment variable in the web.config depending on the profile I choose when deploying?


Solution

  • Take a look at web.config transformations.

    In your Web.Production.Config for production environment set this:

    <add xdt:Transform="Replace" xdt:Locator="Match(key)" key="RunningEnvironment" value="prod"/>
    

    A handy tip you better know is Preview Transform dropdown menu option on Web.<ConfigurationName>.Config file in Visual Studio. There you will see all the changes that are going to take effect.