Multiple environments in ASP.NET Core
By Dawid on (tags: asp.net Core, environment, categories: code)APS.NET Core 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 2017 then we can specify environment in the debug profile of our application:
Some typical settings which we can be controlled by different environments:
- caching
- bundling and minification – for example we can disable it on the debug environment
- friendly error pages instead of stack traces
- logging levels
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.
1: public Startup(IHostingEnvironment env, IApplicationBuilder app)
2: {
3: Configuration = new Configuration().AddJsonFile("config.json").AddEnvironmentVariables();
4: if (env.IsEnvironment("Production"))
5: {
6: app.UseErrorHandler("/Errors/Error");
7: }
8: else
9: {
10: //show all errors in the Development environment
11: app.UseErrorPage(ErrorPageOptions.ShowAll);
12: }
13: }
Please remember that env.IsEnvironment(“environmentname”) will ignore case, so maybe it will better approach in some cases instead of using env.EnvironmentName == “Development”.
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.
1: <environment names="Development">
2: <link rel="stylesheet" href="~/lib/bootstrap/css/bootstrap.css" />
3: <link rel="stylesheet" href="~/lib/bootstrap-touch-carousel/css/bootstrap-touch-carousel.css" />
4: <link rel="stylesheet" href="~/css/site.css" />
5: </environment>
6: <environment names="Staging,Production">
7: <link rel="stylesheet" href="//ajax.aspnetcdn.com/ajax/bootstrap/3.0.0/css/bootstrap.min.css"
8: asp-fallback-href="~/lib/bootstrap/css/bootstrap.min.css"
9: asp-fallback-test-class="hidden" asp-fallback-test-property="visibility" asp-fallback-test-value="hidden" />
10: <link rel="stylesheet" href="//ajax.aspnetcdn.com/ajax/bootstrap-touch-carousel/0.8.0/css/bootstrap-touch-carousel.css"
11: asp-fallback-href="~/lib/bootstrap-touch-carousel/css/bootstrap-touch-carousel.css"
12: asp-fallback-test-class="carousel-caption" asp-fallback-test-property="display" asp-fallback-test-value="none" />
13: <link rel="stylesheet" href="~/css/site.css" />
14: </environment>
david lizhong huang
1/18/2016 9:22 PM
this taghelper does not work in vs2015 community version, i set environment name as development, but it uses production settings in _layout.cshtml, _layout.cshtml does not pick up the the setting in development environmentMirek
1/8/2020 1:39 PM
This is available since ASP.NET Core and not available in ASP.NET MVC 5.Mirek
5/28/2020 12:47 PM
This post was corrected as the described functionality is available in ASP.NET Core not ASP.NET MVC 5