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

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

我目前正在使用asp core v1.1进行项目开发,在我的appsettings.json中,我拥有:

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; } }

然后在Startup.cs中启用了DI以供使用:

And DI enabled to use then in Startup.cs:

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

是否可以通过Controller更改和保存MaxPvPower和MaxWindPower?

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

我尝试使用

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"); }

但是它什么也没做.

推荐答案

此处是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.

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

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.

更多推荐

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

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

发布评论

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

>www.elefans.com

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