且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

代码中的ASP.NET Core appsettings.json更新

更新时间:2023-02-14 14:19:09

此处是Microsoft的有关.Net Core Apps中的配置设置的相关文章:

Here is a relevant article from Microsoft regarding Configuration setup in .Net Core Apps:

Asp.Net核心配置

该页面还具有示例代码可能也有帮助.

The page also has sample code which may also be helpful.

更新

我认为内存提供程序并绑定到POCO类可能有用,但不能按OP预期的那样工作.

I thought In-memory provider and binding to a POCO class might be of some use but does not work as OP expected.

下一个选项可以设置

The next option can be setting reloadOnChange parameter of AddJsonFile to true while adding the configuration file and manually parsing the JSON configuration file and making changes as intended.

    public class Startup
    {
        ...
        public Startup(IHostingEnvironment env)
        {
            var builder = new ConfigurationBuilder()
                .SetBasePath(env.ContentRootPath)
                .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
                .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
                .AddEnvironmentVariables();
            Configuration = builder.Build();
        }
        ...
    }

... reloadOnChange仅在ASP.NET Core 1.1和更高版本中受支持.

... reloadOnChange is only supported in ASP.NET Core 1.1 and higher.