简体   繁体   English

是否可以重命名 ASPNETCORE_ENVIRONMENT 变量?

[英]Is it possible to rename ASPNETCORE_ENVIRONMENT variable?

In Configure method in Startup class of ASP.NET Core WebAPI project ...ASP.NET Core WebAPI project Startup类中的Configure方法中...

public void Configure(IApplicationBuilder app, IWebHostEnvironment env)

injected IWebHostEnvironment is driven by the value in the ASPNETCORE_ENVIRONMENT environment variable.注入的IWebHostEnvironmentASPNETCORE_ENVIRONMENT环境变量中的值驱动。

Can we rename this variable?我们可以重命名这个变量吗? If yes, Is it a good practice to rename it?如果是,重命名它是一个好习惯吗?

You cannot really rename the names of the environment variables ASPNETCORE_ENVIRONMENT or DOTNET_ENVIRONMENT .您不能真正重命名环境变量ASPNETCORE_ENVIRONMENTDOTNET_ENVIRONMENT的名称。 Those are built into the default host builder that you will likely use when creating ASP.NET Core applications.这些内置于您在创建 ASP.NET Core 应用程序时可能会使用的默认主机生成器中。

What you can do however is configure the host with your own configuration sources.但是,您可以做的是使用您自己的配置源配置主机。 That way, you can inject configurations from environment variables that do not follow standard names.这样,您可以从不遵循标准名称的环境变量中注入配置。 For example, if you wanted to read the environment name from the environment variable stage , then you could modify the CreateHostBuilder to include a call to ConfigureHostConfiguration that modifies the configuration:例如,如果您想从环境变量stage读取环境名称,那么您可以修改CreateHostBuilder以包含对修改配置的ConfigureHostConfiguration的调用:

public static IHostBuilder CreateHostBuilder(string[] args) =>
    Host.CreateDefaultBuilder(args)
        .ConfigureHostConfiguration(c => {
            c.AddInMemoryCollection(new Dictionary<string, string>()
            {
                ["Environment"] = Environment.GetEnvironmentVariable("stage"),
            });
        })
        .ConfigureWebHostDefaults(webBuilder =>
        {
            webBuilder.UseStartup<Startup>();
        });

Note that if you call ConfigureHostConfiguration before ConfigureWebHostDefaults , then you will need to make sure that your launchSettings.json does not contain a configuration for the ASPNETCORE_ENVIRONMENT variable or your custom variable will be overwritten.请注意,如果你调用ConfigureHostConfiguration之前ConfigureWebHostDefaults ,那么你需要确保你的launchSettings.json不包含配置为ASPNETCORE_ENVIRONMENT变量或您的自定义变量将被覆盖。 You can also swap those two calls around to make your environment variable take precedence over the standard name.您还可以交换这两个调用,使您的环境变量优先于标准名称。 Of course this only applies in development (since there is no launchSettings.json in production).当然,这仅适用于开发(因为生产中没有launchSettings.json )。

To answer your final question: Is this a good idea?回答你的最后一个问题:这是个好主意吗? Probably not.可能不是。 Unless you need to do this for external reasons (eg to fit into existing deployment processes you don't have control over), you should stick to the default names to make sure that all involved parties understand the impact properly.除非您出于外部原因需要这样做(例如,为了适应您无法控制的现有部署流程),否则您应该坚持使用默认名称以确保所有相关方正确理解影响。

And just to clarify this: Both DOTNET_ENVIRONMENT and ASPNETCORE_ENVIRONMENT are good to use and the latter is not going away any time soon.只是为了澄清这一点: DOTNET_ENVIRONMENTASPNETCORE_ENVIRONMENT都很好用,后者不会很快消失。

There is a built in way to set the environment for the application:有一种内置的方式来设置应用程序的环境:

public static IHostBuilder CreateHostBuilder(string[] args) =>
        Host.CreateDefaultBuilder(args)
            .UseEnvironment("WHAT EVER YOU WANT")//set it for this app
            .ConfigureWebHostDefaults(webBuilder =>
            {
              webBuilder.UseStartup<Startup>();
            });

As you can see you can use 'UseEnvironment("MY COOL ENVIRONMENT")' to set it to what ever you want.如您所见,您可以使用 'UseEnvironment("MY COOL ENVIRONMENT")' 将其设置为您想要的任何值。

And the source of it can be any environment variable, file, database, hard coded... any thing you can think of.它的来源可以是任何环境变量、文件、数据库、硬编码……任何你能想到的东西。

Here is some documentation.这是一些文档。 Scroll to the section called EnvironmentName to find information about it.滚动到名为EnvironmentName的部分以查找有关它的信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在运行时设置 ASPNETCORE_ENVIRONMENT 变量 - How to set ASPNETCORE_ENVIRONMENT variable at runtime ".NET Core 控制台应用程序和 aspnetcore_environment 变量" - .NET Core console app and aspnetcore_environment variable 如何为控制台应用程序设置 ASPNETCORE_ENVIRONMENT? - How to set ASPNETCORE_ENVIRONMENT for console application? 在T4模板中获取ASPNETCORE_ENVIRONMENT? - Get ASPNETCORE_ENVIRONMENT in T4 Template? 创建新的ASPNETCORE_ENVIRONMENT进行开发 - Create new ASPNETCORE_ENVIRONMENT for Development 在构建配置中设置aspnetcore_environment - Set aspnetcore_environment within build configuration 为什么在Azure上设置ASPNETCORE_ENVIRONMENT变量时需要发布我的project.json - Why do I need to publish my project.json when setting ASPNETCORE_ENVIRONMENT variable on Azure 为什么 Webservice 不使用来自 web.config 的变量 AS.NETCORE_ENVIRONMENT - Why Webservice doesn't use variable ASPNETCORE_ENVIRONMENT from web.config 如何在 AzureDevops 中为 Dockerfile 设置环境变量(ASPNETCORE_ENVIRONMENT)? - How to set environment variables (ASPNETCORE_ENVIRONMENT) in AzureDevops for Dockerfile? AS.NETCORE_ENVIRONMENT 不再覆盖 DO.NET_ENVIRONMENT? - ASPNETCORE_ENVIRONMENT no longer overrides DOTNET_ENVIRONMENT?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM