ASP.NET Core appsettings.json 更新代码

编程入门 行业动态 更新时间:2024-10-24 12:26:59
本文介绍了ASP.NET Core appsettings.json 更新代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我目前正在使用 asp 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"));

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

我尝试使用

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 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 更新代码

本文发布于:2023-11-02 14:59:52,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1552705.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:代码   NET   ASP   Core   json

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!