RC2之后
那么如何设置不同的EnvironmentName呢?
Set the ASPNETCORE_ENVIRONMENT
环境变量。
设置该环境变量的方法有很多。这些包括launchSettings.json
简介和其他特定于环境的方式 https://docs.asp.net/en/latest/fundamentals/environments.html。这里有些例子。
从控制台:
// PowerShell
> $env:ASPNETCORE_ENVIRONMENT="Development"
// Windows Command Line
> SET ASPNETCORE_ENVIRONMENT=Development
// Bash
> ASPNETCORE_ENVIRONMENT=Development
从 Azure Web 应用程序的应用程序设置:
RC2 之前
我可以想象它应该在“命令”中设置为服务器的参数。
那是真实的。在您的project.json中,添加--ASPNET_ENV production
作为服务器的参数。
"commands": {
"web": "Microsoft.AspNet.Hosting --ASPNET_ENV production --server Microsoft.AspNet.Server.WebListener --server.urls http://localhost:5001"
}
现在当你跑步时dnx . web
从命令行,ASPNET_ENV
将production
.
相关ASP.NET Core托管源代码
The WebHostBuilder
联合起来"ASPNETCORE_"
与WebHostDefaults.EnvironmentKey
使"ASPNETCORE_environment"
。它还支持传统密钥。
WebHostDefaults.cs https://github.com/aspnet/Hosting/blob/master/src/Microsoft.AspNetCore.Hosting.Abstractions/WebHostDefaults.cs
namespace Microsoft.AspNetCore.Hosting
{
public static class WebHostDefaults
{
public static readonly string ApplicationKey = "applicationName";
public static readonly string StartupAssemblyKey = "startupAssembly";
public static readonly string DetailedErrorsKey = "detailedErrors";
public static readonly string EnvironmentKey = "environment";
public static readonly string WebRootKey = "webroot";
public static readonly string CaptureStartupErrorsKey = "captureStartupErrors";
public static readonly string ServerUrlsKey = "urls";
public static readonly string ContentRootKey = "contentRoot";
}
}
WebHostBuilder.cs https://github.com/aspnet/Hosting/blob/master/src/Microsoft.AspNetCore.Hosting/WebHostBuilder.cs
_config = new ConfigurationBuilder()
.AddEnvironmentVariables(prefix: "ASPNETCORE_")
.Build();
if (string.IsNullOrEmpty(GetSetting(WebHostDefaults.EnvironmentKey)))
{
// Try adding legacy environment keys, never remove these.
UseSetting(WebHostDefaults.EnvironmentKey,
Environment.GetEnvironmentVariable("Hosting:Environment")
?? Environment.GetEnvironmentVariable("ASPNET_ENV"));
}
向后兼容性
环境密钥设置为ASPNETCORE_ENVIRONMENT
环境变量。ASPNET_ENV
and Hosting:Environment
仍然受支持,但会生成一条已弃用的消息警告。
https://docs.asp.net/en/latest/migration/rc1-to-rtm.html https://docs.asp.net/en/latest/migration/rc1-to-rtm.html
默认值
默认值为“生产”并设置在这里。 https://github.com/aspnet/Hosting/blob/b6da89f54cff11474f17486cdc55c2f21f2bbd6b/src/Microsoft.AspNetCore.Hosting/Internal/HostingEnvironment.cs#L10