Author : MD TAREQ HASSAN

Details: https://docs.microsoft.com/en-us/aspnet/core/fundamentals/environments

ASPNETCORE_ENVIRONMENT variable

Setting environment variable

1. Project properties

2. launchSettings
Solution explorer > Show all files > Properties > launchSettings.json


  "profiles": {
    "IIS Express": {
      
	  // ... ... ...
	  
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    }
    

Notes:

Multiple appsettings json

appsettings for corresponding environment

ASPNETCORE_ENVIRONMENT appsettings
not set appsettings.json
Development appsettings.Development.json
Production appsettings.Production.json
Foo appsettings.Foo.json

ExceptionHandler depending on environment

Startup.cs

  public void Configure(IApplicationBuilder app, IHostingEnvironment env)
  {
      if (env.IsDevelopment())
      {
          app.UseDeveloperExceptionPage();
      }

      if (env.IsProduction() || env.IsStaging() || env.IsEnvironment("Staging_2"))
      {
          app.UseExceptionHandler("/Error");
      }
      
      // ... ... ...
  }