EP 0: How to set environment variable ?

Welcome Note

It is my pleasure to welcome you to the first episode of Waseem’s .NET Weekly newsletter and thank you very much for subscribing.

Providing you with knowledge and helping you to become a better Software Engineer is my goal.

What is launchSettings.json file ?

This file is automatically created after you create a project and it can be found in the properties section of your project, it contains configurations to launch settings for IIS. It contains an SSL port, application URL and launch URL of your project, by default launch URL used to contain weather forecasts 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.

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.

As we know our 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 the 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?

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

  2. The 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 the command prompt then you can use this command.

set ASPNETCORE_Environment = Production //For Windows

export ASPNETCORE_Environment = Production //For Linux

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

There are 3 ways you can help me in growing:

  1. Subscribe to my YouTube Channel

  2. Subscribe to my Weekly .NET Newsletter of C#/.NET

  3. Download my eBook at Gum Road which contains 30+ .NET Tips