Multiple environments in ASP.NET 5

By Dawid on (tags: 5, environment, categories: code)

APS.NET 5 has introduced new environment variable called ASPNET_ENV. It allows us to control our application behaviour between multiple environments because it’s describing environment on which application is currently working on.

We can put there whatever we want but typically it’s set to:

  • Development
  • Staging
  • Production

If we are working in Visual Studio 2015 then we can specify environment in the debug profile of our application:

Some typical settings which we can be controlled by different environments:

Using environment settings in code

If there is need of checking on which environment out application is currently working we can use this through IHostingEnvironment service. It’s provided by ASP.NET hosting layer and can be easily used by injecting it with Dependency Injection.

Please remember that will ignore case, so maybe it will better approach in some cases instead of using

Environment Tag Helper

If we can reference different scripts and style sheets (depending on the environment) in the Razor view then we can use environment tag helper. This tag is basing on current value of ASPNET_ENV variable. Below is an example usage of that tag helper.