C# ASP.NET Core appsettings.json未加载正确的设置

C# ASP.NET Core appsettings.json未加载正确的设置,c#,asp.net-core,asp.net-core-webapi,asp.net-core-configuration,C#,Asp.net Core,Asp.net Core Webapi,Asp.net Core Configuration,在我的Azure中,我有ENVIRONMENT=Development,但我的设置没有加载 public static IConfiguration Configuration { get; } = new ConfigurationBuilder() .SetBasePath(Directory.GetCurrentDirectory()) .AddJsonFile("appsettings.json", optional: true, reloadOnChange

在我的Azure中,我有
ENVIRONMENT=Development
,但我的设置没有加载

public static IConfiguration Configuration { get; } = new ConfigurationBuilder()
       .SetBasePath(Directory.GetCurrentDirectory())
       .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true) // reloadOnChange Whether the configuration should be reloaded if the file changes.
       .AddJsonFile($"appsettings.{Environment.GetEnvironmentVariable("ENVIRONMENT")}.json", optional: true, reloadOnChange: true)
       .AddEnvironmentVariables() // Environment Variables override all other, ** THIS SHOULD ALWAYS BE LAST
       .Build();

但它始终使用默认设置。

请更新您的下一行:

.SetBasePath(env.ContentRootPath)

这是我在部署到azure时配置startup.cs的方式

public Startup(
    IConfiguration configuration,
    IHostingEnvironment hostingEnvironment)
{
    _configuration = configuration;
    _hostingEnvironment = hostingEnvironment;

    var builder = new ConfigurationBuilder();

    if (_hostingEnvironment.IsDevelopment())
    {
        builder.AddUserSecrets<Startup>();
    }
    else
    {
        builder
            .SetBasePath(hostingEnvironment.ContentRootPath)
            .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
            .AddJsonFile($"appsettings.{hostingEnvironment.EnvironmentName}.json", optional: true)
            .AddEnvironmentVariables()
            .Build();
    }
}
如果您在我的程序中仍然有任何问题,请告诉我。cs我有

public static IConfiguration Configuration { get; } = new ConfigurationBuilder()
            .SetBasePath(Directory.GetCurrentDirectory())
            .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true) // reloadOnChange Whether the configuration should be reloaded if the file changes.
            .AddJsonFile($"appsettings.{Environment.GetEnvironmentVariable("ENVIRONMENT")}.json", optional: true, reloadOnChange: true)
            .AddEnvironmentVariables() // Environment Variables override all other, ** THIS SHOULD ALWAYS BE LAST
            .Build();
比我做的还要多

public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
        WebHost.CreateDefaultBuilder(args) // Sets up the default order in which all the configurations are read
            .UseStartup<Startup>()
            .ConfigureAppConfiguration((c, x) =>
            {

                x.AddConfiguration(Configuration); <-------

            })
            .UseSerilog((h, c) => c.Enrich.FromLogContext().WriteTo.Sentry(s =>
            {

                s.Dsn = new Sentry.Dsn(Configuration.GetSection("Sentry:Dsn").Value);
                s.MinimumEventLevel = Serilog.Events.LogEventLevel.Error;
                s.MinimumBreadcrumbLevel = Serilog.Events.LogEventLevel.Information;

            })).UseSentry(x =>
            {
                x.IncludeRequestPayload = true;
            });
公共静态IWebHostBuilder CreateWebHostBuilder(字符串[]args)=>
WebHost.CreateDefaultBuilder(args)//设置读取所有配置的默认顺序
.UseStartup()
.ConfigureAppConfiguration((c,x)=>
{
x、 AddConfiguration(配置);c.rich.FromLogContext().WriteTo.Sentry(s=>
{
s、 Dsn=new Sentry.Dsn(Configuration.GetSection(“Sentry:Dsn”).Value);
s、 MinimumEventLevel=Serilog.Events.LogEventLevel.Error;
s、 minimumbreadcrumbleLevel=Serilog.Events.LogEventLevel.Information;
})).UseSentry(x=>
{
x、 IncludeRequestPayload=true;
});

您是否尝试过设置
ASPNETCORE\u环境
?您是如何以及在何处设置环境变量的?我如何在我的程序中设置此变量。cs,我在哪里做所有事情?我在我的程序中做类似的事情。cs,我如何做?
public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
        WebHost.CreateDefaultBuilder(args) // Sets up the default order in which all the configurations are read
            .UseStartup<Startup>()
            .ConfigureAppConfiguration((c, x) =>
            {

                x.AddConfiguration(Configuration); <-------

            })
            .UseSerilog((h, c) => c.Enrich.FromLogContext().WriteTo.Sentry(s =>
            {

                s.Dsn = new Sentry.Dsn(Configuration.GetSection("Sentry:Dsn").Value);
                s.MinimumEventLevel = Serilog.Events.LogEventLevel.Error;
                s.MinimumBreadcrumbLevel = Serilog.Events.LogEventLevel.Information;

            })).UseSentry(x =>
            {
                x.IncludeRequestPayload = true;
            });