Default ASP.NET Core web project contain such lines in Startup.cs
:
if (string.Equals(env.EnvironmentName, \"Development\", StringComparison.Ordi
if you need to set this without changing code - from the command prompt at the root of the project source folder type:
set ASPNET_ENV=Debug
You set the environment by defining an environment variable named ASPNET_ENV
.
For example, if you want Release SET ASPNET_ENV=Release
.
It might also work if you pass ASPNET_ENV=Release
as parameter to the commands but I cannot check it now.
Here is how it is implemented: https://github.com/aspnet/Hosting/blob/217f9ca3d3ccf59ea06e6555820974ba9c3b5932/src/Microsoft.AspNet.Hosting/ConfigureHostingEnvironment.cs
If you are thinking that from where it takes this value then as this moment it is static and default value is development.
https://github.com/aspnet/Hosting/blob/dev/src/Microsoft.AspNet.Hosting/HostingEnvironment.cs
When you look at IHostingEnviroment variable type then it is Microsoft.AspNet.Hosting.HostingEnvrioment.
There are two ways you can now change as per dynamic configuration.
You can implement IHostingEnvironment interface and use your own type for that. You can read value from Config file.
You can use interface You can update that variable directly over here.
public Startup(IHostingEnvironment env)
{
// Setup configuration sources.
Configuration = new Configuration()
.AddJsonFile("config.json").AddEnvironmentVariables();
Configuration.Set("ASPNET_ENV","Your own value");
}
If you look at services in ConfigureServices there is list of service configure by default and one of them is IConfigureHostingEnviroment. Default implementation is internal class so you can not directly access but you can set above key ASPNET_ENV and it read that value.
https://github.com/aspnet/Hosting/blob/dev/src/Microsoft.AspNet.Hosting/ConfigureHostingEnvironment.cs
If you prefer to use VS features (e.g. VS 2017), it is possible to add Environment variables in the Debug tab of project properties. For example, on the latest ASP.NET Core versions (after RC2) you should set ASPNETCORE_ENVIRONMENT
variable.
As result, the launchSettings.json
file will be created (or updated) in the Properties folder of the corresponding project, so it will be easy to persist this file into your source control solution and share between developers (as opposite to other solutions with SET
/ SETX
commands)
Note: by default, latest ASP.NET Core set the environment to Production. So, you just need to set ASPNETCORE_ENVIRONMENT
to Development
in VS for Debugging purposes (see screenshot above). And sure, when you want to run your code locally with Staging environment, you should set ASPNETCORE_ENVIRONMENT
to Staging
. And finally, when you want to run it on Production environment, just remove this variable or set value to Production
.
To summarize: just make sure Development, Staging or Production values are used (not 'Dev' or anything else) in Debug dialog to set environment and make different extensions working.
See also relevant source code from ASP.NET Core:
namespace Microsoft.AspNetCore.Hosting
{
/// <summary>Commonly used environment names.</summary>
public static class EnvironmentName
{
public static readonly string Development = "Development";
public static readonly string Staging = "Staging";
public static readonly string Production = "Production";
}
}
namespace Microsoft.AspNetCore.Hosting
{
/// <summary>
/// Extension methods for <see cref="T:Microsoft.AspNetCore.Hosting.IHostingEnvironment" />.
/// </summary>
public static class HostingEnvironmentExtensions
{
/// <summary>
/// Checks if the current hosting environment name is "Development".
/// </summary>
/// <param name="hostingEnvironment">An instance of <see cref="T:Microsoft.AspNetCore.Hosting.IHostingEnvironment" />.</param>
/// <returns>True if the environment name is "Development", otherwise false.</returns>
public static bool IsDevelopment(this IHostingEnvironment hostingEnvironment)
{
if (hostingEnvironment == null)
throw new ArgumentNullException("hostingEnvironment");
return hostingEnvironment.IsEnvironment(EnvironmentName.Development);
}
/// <summary>
/// Checks if the current hosting environment name is "Staging".
/// </summary>
/// <param name="hostingEnvironment">An instance of <see cref="T:Microsoft.AspNetCore.Hosting.IHostingEnvironment" />.</param>
/// <returns>True if the environment name is "Staging", otherwise false.</returns>
public static bool IsStaging(this IHostingEnvironment hostingEnvironment)
{
if (hostingEnvironment == null)
throw new ArgumentNullException("hostingEnvironment");
return hostingEnvironment.IsEnvironment(EnvironmentName.Staging);
}
/// <summary>
/// Checks if the current hosting environment name is "Production".
/// </summary>
/// <param name="hostingEnvironment">An instance of <see cref="T:Microsoft.AspNetCore.Hosting.IHostingEnvironment" />.</param>
/// <returns>True if the environment name is "Production", otherwise false.</returns>
public static bool IsProduction(this IHostingEnvironment hostingEnvironment)
{
if (hostingEnvironment == null)
throw new ArgumentNullException("hostingEnvironment");
return hostingEnvironment.IsEnvironment(EnvironmentName.Production);
}
/// <summary>
/// Compares the current hosting environment name against the specified value.
/// </summary>
/// <param name="hostingEnvironment">An instance of <see cref="T:Microsoft.AspNetCore.Hosting.IHostingEnvironment" />.</param>
/// <param name="environmentName">Environment name to validate against.</param>
/// <returns>True if the specified name is the same as the current environment, otherwise false.</returns>
public static bool IsEnvironment(this IHostingEnvironment hostingEnvironment, string environmentName)
{
if (hostingEnvironment == null)
throw new ArgumentNullException("hostingEnvironment");
return string.Equals(hostingEnvironment.EnvironmentName, environmentName, StringComparison.OrdinalIgnoreCase);
}
}
}
Here is one more way to set and switch ASPNETCORE_ENVIRONMENT variable in VS2017 (addtional note to @clark-wu answer):
Note: launchSettings.json has two profiles in my case: "IISExpress" and "Project" where ASPNETCORE_ENVIRONMENT is defined.
{
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:10000/",
"sslPort": 0
}
},
"profiles": {
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"launchUrl": "api/entities",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development" // <-- related to IIS Express profile
}
},
"Project": {
"commandName": "Project",
"launchBrowser": true,
"launchUrl": "api/entities",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Production" // <-- related to Project profile
},
"applicationUrl": "http://localhost:10000/"
}
}
}
Official documentation: You can set ASPNETCORE_ENVIRONMENT to any value, but three values are supported by the framework: Development, Staging, and Production. If ASPNETCORE_ENVIRONMENT isn't set, it defaults to Production.
In VsCode add the following to launch.json
{
"version": "0.2.0",
"configurations": [
{
...
"env": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
...
]
}