如何将默认设置从appsettings设置为API模型上的属性

编程入门 行业动态 更新时间:2024-10-24 15:17:45
本文介绍了如何将默认设置从appsettings设置为API模型上的属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

如何在由.NET Core 3框架实例化的模型上,从我的appsettings.json文件中读取属性,将其设置为默认值?

How can I set a default value to a property, reading it from my appsettings.json file, on a model that is instantiated by the .NET Core 3 framework?

我创建了一个仓库(一个全新的.NET Core 3项目),在其中尝试说明问题: github/NelsonPRSousa/dependency-injection-default-constructor

I've created a repo (a completely new .NET Core 3 project) where I try to illustrate the problem: github/NelsonPRSousa/dependency-injection-default-constructor

API操作:

[HttpGet] public IEnumerable<WeatherForecast> Get([FromQuery] FilteringRequestModel request) { var defaultType = request.Type; var rng = new Random(); return Enumerable.Range(1, 5).Select(index => new WeatherForecast { Date = DateTime.Now.AddDays(index), TemperatureC = rng.Next(-20, 55), Summary = Summaries[rng.Next(Summaries.Length)] }) .ToArray(); }

型号:

public class FilteringRequestModel { /* Please note that we must have a parameterless constructor, since it's the framework responsability to instatiate this object * when invoking this action: IEnumerable<WeatherForecast> Get([FromQuery] FilteringRequestModel request) */ public FilteringRequestModel() { //Type = System.Configuration.ConfigurationManager.AppSettings["DefaultTypeTopRated"]; } public string Type { get; set; } = "top_rated"; // TODO: Read from appsettings }

推荐答案

您可以将IOptions与依赖注入一起使用

You could use IOptions with Dependecy Injection

更新 FilteringRequestModel 类

public class FilteringRequestModel { public FilteringRequestModel() { } public void Initialize(IOptions<FilteringRequestSettings> settings) { if (string.IsNullOrEmpty(this.Type)) { this.Type = settings.Value.Type; } } public string Type { get; set; } }

添加一个 FilteringRequestSettings 类

public class FilteringRequestSettings { public string Type { get; set; } }

像这样更新 appsettings.Development.json

{ "Logging": { "LogLevel": { "Default": "Debug", "System": "Information", "Microsoft": "Information" } }, "FilteringRequest": { "Type": "foo" } }

像这样

更新 Startup 类中的 ConfigureServices 方法

public void ConfigureServices(IServiceCollection services) { services.Configure<FilteringRequestSettings>(this.Configuration.GetSection("FilteringRequest")); services.AddControllers(); }

最后,像这样更新 WeatherForecastController 类

[HttpGet] public IEnumerable<WeatherForecast> Get([FromQuery] FilteringRequestModel request) { request.Initialize(_settings); var defaultType = request.Type; var rng = new Random(); return Enumerable.Range(1, 5).Select(index => new WeatherForecast { Date = DateTime.Now.AddDays(index), TemperatureC = rng.Next(-20, 55), Summary = Summaries[rng.Next(Summaries.Length)] }) .ToArray(); }

说明

Explanation

  • 您将从Appsettings中加载参数到 IOptions< FilteringRequestSettings>
  • IOptions< FilteringRequestSettings> 将被注入 WeatherForecastController
  • 然后,您将在 request 中调用 Initialize 方法 IOptions< FilteringRequestSettings>
  • 的参数
  • Initialize 方法验证是否尚未设置(从查询/请求中) Type 属性.如果不是,它将使用appsettings中的值(通过 IOptions< FilteringRequestSettings>
  • You will load parameter from appsettings into IOptions<FilteringRequestSettings>
  • IOptions<FilteringRequestSettings> will be injected into WeatherForecastController
  • You will then call the Initialize method your request parameter with IOptions<FilteringRequestSettings>
  • The Initialize method verify if the Type property has not already been set (from query / request). If not, it will use the value from appsettings (through IOptions<FilteringRequestSettings>

更多推荐

如何将默认设置从appsettings设置为API模型上的属性

本文发布于:2023-11-25 13:49:54,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1630010.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:设置为   如何将   默认设置   属性   模型

发布评论

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

>www.elefans.com

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