How to set environment variable?

What is launchSettings.json file ?

This file is automatically created after you create project and it can be found in properties section of your project , it contains configurations to launch settings for IIS. It contains SSL port , application URL and launch URL of your project , by default launch URL used to contain weatherforecast but now you would see swagger in it. To run your project locally your launchBrowser should be true under your project name configuration otherwise applicationUrl ( https://localhost:7265 ) will not work.

How to set environment vairables in .NET

What is appSettings.json file ?

appsettings.json is auto created when we create new project. It contains all settings of our project e.g. (connection strings, logging details , JWT configurations , secret keys and CORS policies etc.). This file has three variants e.g. (Development, Staging and Production) which looks like this.

How to set environment vairables in .NET

As we know that our all environments have different ports, different connection strings and configurations, so we use different files for each environment and put information in relevant files.

How to set environment locally ?

We can set that on which environment our application will run using ASPNETCORE_ENVIRONMENT property in launchSettings.json file.

If you working locally and you have to configure the staging environment or production environment how would you you do !

One way is that you can copy the all configurations of production and paste them in root file appsettings.json which is wrong because it can create complexities you have to clearly look into each property .

Best way is to just change the value of ASPNETCORE_ENVIRONMENT in launchSettings.json from Development to Production and here it is you are fine to go.

If you want to set the variable through command prompt then you can use this command .

For Windows

set ASPNETCORE_Environment = Production

For Linux

export ASPNETCORE_Environment = Production

Our .NET Core application will check the value of this variable and act accordingly.

This article was originally published at https://mwaseemzakir.substack.com/ on .

Whenever you're ready, there are 3 ways I can help you:

  1. Subscribe to my youtube channel : For in-depth tutorials, coding tips, and industry insights.
  2. Promote yourself to 9,000+ subscribers : By sponsoring this newsletter
  3. Patreon community : Get access to all of my blogs and articles at one place