如何在由.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模型上的属性
发布评论