[英]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.注入的
IWebHostEnvironment
由ASPNETCORE_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_ENVIRONMENT
或DOTNET_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_ENVIRONMENT
和ASPNETCORE_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.