ASP.NET Core appsettings.json 更新代码 [英] ASP.NET Core appsettings.json update in code

查看:35
本文介绍了ASP.NET Core appsettings.json 更新代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在使用 asp.net core v1.1 进行项目,并且在我的 appsettings.json 中有:

应用设置":{"AzureConnectionKey": "***","AzureContainerName": "**","NumberOfTicks": 621355968000000000,"NumberOfMiliseconds": 10000,"SelectedPvInstalationIds": [ 13, 137, 126, 121, 68, 29 ],最大PvPower":160,最大风力":5745.35},

我也有用于存储它们的类:

公共类AppSettings{公共字符串 AzureConnectionKey { 获取;放;}公共字符串 AzureContainerName { get;放;}公共长 NumberOfTicks { 得到;放;}公共长 NumberOfMiliseconds { 获取;放;}public int[] SelectedPvInstalationIds { get;放;}公共十进制 MaxPvPower { 获取;放;}公共十进制 MaxWindPower { 获取;放;}}

然后在 Startup.cs 中启用 DI:

services.Configure(Configuration.GetSection("AppSettings"));

有什么办法可以改变和保存MaxPvPowerMaxWindPower 的控制器?

我尝试使用

private readonly AppSettings _settings;公共 HomeController(IOptions 设置){_settings = settings.Value;}[授权(政策=管理政策")]公共 IActionResult 更新设置(十进制 pv,十进制风){_settings.MaxPvPower = pv;_settings.MaxWindPower = 风;返回重定向(设置");}

但它什么也没做.

解决方案

这里是微软关于 .Net Core Apps 中的配置设置的相关文章:

Asp.Net 核心配置

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

更新

我认为 内存中提供程序和绑定到 POCO 类 可能有一些用处,但不能按预期工作.

下一个选项可以设置 reloadOnChange 参数rel="nofollow noreferrer">AddJsonFile 在添加配置文件和手动解析 JSON 配置文件并按预期进行更改.

 公共类启动{...公共启动(IHostingEnvironment env){var builder = new ConfigurationBuilder().SetBasePath(env.ContentRootPath).AddJsonFile(appsettings.json", 可选: true, reloadOnChange: true).AddJsonFile($"appsettings.{env.EnvironmentName}.json", 可选:true).AddEnvironmentVariables();配置 = builder.Build();}...}

<块引用>

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

I am currently working on project using asp.net core v1.1, and in my appsettings.json I have:

"AppSettings": {
   "AzureConnectionKey": "***",
   "AzureContainerName": "**",
   "NumberOfTicks": 621355968000000000,
   "NumberOfMiliseconds": 10000,
   "SelectedPvInstalationIds": [ 13, 137, 126, 121, 68, 29 ],
   "MaxPvPower": 160,
   "MaxWindPower": 5745.35
},

I also have class that I use to store them:

public class AppSettings
{
    public string AzureConnectionKey { get; set; }
    public string AzureContainerName { get; set; }
    public long NumberOfTicks { get; set; }
    public long NumberOfMiliseconds { get; set; }
    public int[] SelectedPvInstalationIds { get; set; }
    public decimal MaxPvPower { get; set; }
    public decimal MaxWindPower { get; set; }
}

And DI enabled to use then in Startup.cs:

services.Configure<AppSettings>(Configuration.GetSection("AppSettings"));

Is there any way to change and save MaxPvPower and MaxWindPower from Controller?

I tried using

private readonly AppSettings _settings;

public HomeController(IOptions<AppSettings> settings)
{
    _settings = settings.Value;
}

[Authorize(Policy = "AdminPolicy")]
 public IActionResult UpdateSettings(decimal pv, decimal wind)
 {
    _settings.MaxPvPower = pv;
    _settings.MaxWindPower = wind;

    return Redirect("Settings");
 }

But it did nothing.

解决方案

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

Asp.Net Core Configuration

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

Update

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 is only supported in ASP.NET Core 1.1 and higher.

这篇关于ASP.NET Core appsettings.json 更新代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆