Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
231 views
in Technique[技术] by (71.8m points)

c# - How to set Environment Name (IHostingEnvironment.EnvironmentName)?

Default ASP.NET Core web project contain such lines in Startup.cs:

if (string.Equals(env.EnvironmentName, "Development", StringComparison.OrdinalIgnoreCase))
{
    app.UseBrowserLink();
    app.UseDeveloperExceptionPage(ErrorPageOptions.ShowAll);
}
else
{
    app.UseExceptionHandler("/Home/Error");
}

As I understand, the EnvironmentName is a new way to handle Dev/Production environment. But it doesn't changes on Release build configuration. So what is the way to set a different EnvironmentName?

I can imagine that it should be set in "Commands" as a parameter for server.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

After RC2

So what is the way to set a different EnvironmentName?

Set the ASPNETCORE_ENVIRONMENT environmental variable.

There are many ways to set that environmental variable. These include a launchSettings.json profile and other environment-specific ways. Here are some examples.

From a console:

// PowerShell
> $env:ASPNETCORE_ENVIRONMENT="Development"

// Windows Command Line
> SET ASPNETCORE_ENVIRONMENT=Development

// Bash
> ASPNETCORE_ENVIRONMENT=Development

From an Azure Web App's App settings:

Set Environment Name in Azure

Before RC2

I can imagine that it should be set in "Commands" as a parameter for server.

That is true. In your project.json, add --ASPNET_ENV production as a parameter for the server.

"commands": {
  "web": "Microsoft.AspNet.Hosting --ASPNET_ENV production --server Microsoft.AspNet.Server.WebListener --server.urls http://localhost:5001"
}

Now when you run dnx . web from the command line, ASPNET_ENV will be production.

Relevant ASP.NET Core Hosting Source Code

The WebHostBuilder combines "ASPNETCORE_" with the WebHostDefaults.EnvironmentKey to make "ASPNETCORE_environment". It also supports the legacy keys.

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

_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"));
}

Backward Compatibility

The environment key is set with the ASPNETCORE_ENVIRONMENT environment variable. ASPNET_ENV and Hosting:Environment are still supported, but generate a deprecated message warning.

https://docs.asp.net/en/latest/migration/rc1-to-rtm.html

Default Value

The default value is "Production" and is set here.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...