在 Rider 中将 HostingEnvironment.EnvironmentName 设置为 Development

Mug*_*gen 5 .net .net-core rider

我的代码:

    public static async Task Main(string[] args)
    {
        var host = new HostBuilder()
            .ConfigureAppConfiguration(
                (hostContext, configApp) =>
                {
                    configApp.SetBasePath(Directory.GetCurrentDirectory());
                    configApp.AddJsonFile("appsettings.json");
                    configApp.AddJsonFile(
                        $"appsettings.{hostContext.HostingEnvironment.EnvironmentName}.json",
                        optional: true);
                })
            .Build();

        await host.RunAsync();
    }
Run Code Online (Sandbox Code Playgroud)

在运行时我看到的价值HostingEnvironment.EnvironmentNameProduction

我尝试在运行配置中设置以下环境变量,但它没有更改运行时:ASPNETCORE_ENVIRONMENT=Development.

我在哪里可以配置它?

Mug*_*gen 5

添加以下内容解决了该问题,我在使用反编译的 .NET 源代码进行调试后发现它。它似乎没有记录在任何地方,或者我遗漏了一些东西。

    public static async Task Main(string[] args)
    {
        var host = new HostBuilder()
            .ConfigureHostConfiguration(configHost => configHost.AddEnvironmentVariables())
            .ConfigureAppConfiguration(
                (hostContext, configApp) =>
                {
                    configApp.SetBasePath(Directory.GetCurrentDirectory());
                    configApp.AddJsonFile("appsettings.json");
                    configApp.AddJsonFile(
                        $"appsettings.{hostContext.HostingEnvironment.EnvironmentName}.json",
                        optional: true);
                })
            .Build();

        await host.RunAsync();
    }
Run Code Online (Sandbox Code Playgroud)

添加的线路是ConfigureHostConfiguration呼叫。

  • 我明白你的意思。我知道这会很快变得令人困惑。很高兴您找到了问题的解决方案。快乐编码。 (2认同)