繁体   English   中英

如何在 C# 中的视图模型上从 AppSettings 设置默认值?

[英]How to set default value from AppSettings on a view model in C#?

我正在公开一个新 API 的视图,并想知道如何为属性设置默认值,从我的 appsettings.json 文件中读取它。 我该怎么做?

我试过在视图上使用依赖注入,但似乎不可能,因为它是实例化类的框架。

public class FilteringRequest
    {
        private readonly IAppContext appContext;

        public FilteringRequest(IAppContext appContext)
        {
            this.appContext = appContext;
        }

        // TODO: appsettings?
        // Perhaps something like appContext.DefaultType
        public string Type { get; set; } = "top_rated";

        // ...
}

这种方法不起作用,因为我相信框架需要一个无参数的默认构造函数。

An unhandled exception occurred while processing the request.
InvalidOperationException: Could not create an instance of type 'Wanderlust.API.Models.Requests.FilteringRequest'. Model bound complex types must not be abstract or value types and must have a parameterless constructor. Alternatively, give the 'request' parameter a non-null default value.
Microsoft.AspNetCore.Mvc.ModelBinding.Binders.ComplexTypeModelBinder.CreateModel(ModelBindingContext bindingContext)

请记住,IAppContext 已经具有在 appsettings.json 文件中定义的所有属性。

依赖注入应该有效。 在 Startup.cs 中

public void ConfigureServices(IServiceCollection services)
{
    // ...
    services.Configure<MyAppContext>(Configuration.GetSection(nameof(MyAppContext)));
    // ...
}

定义你的类 MyAppContext:

public class MyAppContext
{
    public MyAppContext()
    {
    }

    public string DefaultType { get; set; }
    // other public properties here...
}

在您的 FilteringRequest 类中:

public class FilteringRequest
{
    private readonly MyAppContext _appContext;

    public FilteringRequest(Microsoft.Extensions.Options.IOptions<MyAppContext> appContext)
    {
        _appContext = appContext.Value;
    }

    string _type;
    public string Type
    {
        get => _type ?? (_type = _appContext.DefaultType);
        set => _type = value;
    }
    // ...
}

要从任何类中的 appsettings.json 读取数据,您只需要通过创建IConfiguration字段来传递具有依赖项注入的Configuration

public class FilteringRequest
{       
    private readonly IConfiguration _configuration;

    public FilteringRequest(IConfiguration configuration)
    {
        _configuration = configuration;
        Type = _configuration["Model:FilteringRequest:Type"];//set value from appsettings.json file
      
    }
 
    public string Type { get; set; } 

    // ...
}

appsettings.json:

{
"Model": {
  "FilteringRequest": {
    "Type": "testdata"
  }
}
}

控制器:

public class HomeController : Controller
{      
    private readonly IConfiguration _configuration;
    public HomeController (IConfiguration configuration)
    {
        _configuration = configuration;
    }
    public IActionResult Index
    {
        var modelinstance = new FilteringRequest(_configuration);//create the viewmodel with default 'Type"
        return View();
    }
}

参考:

从 .net core 中的 appsettings.json 获取价值

从 .NET Core 2 中的类中读取 appsettings.json

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM