【问题标题】:How to set default value from AppSettings on a view model in C#?如何在 C# 中的视图模型上从 AppSettings 设置默认值?
【发布时间】:2020-01-14 11:56:15
【问题描述】:

我正在公开一个关于新 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 文件中定义的所有属性。

【问题讨论】:

    标签: c# asp.net-core default-value


    【解决方案1】:

    依赖注入应该可以工作。在 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;
        }
        // ...
    }
    

    【讨论】:

    • 我不知道 IOptions 做了什么,但我收到了这个错误:错误 CS0310 'AppContext' must be a non-abstract type with a public parameterless constructor才能将其用作参数'TOptions'在泛型类型或方法 'IOptions'
    • IOptions 用于检索配置的类实例,这些实例映射到 appsettings.json 文件中定义的配置选项以及其他来源。您说“IAppContext 已经具有 appsettings.json 文件中定义的所有属性”。由于 IAppContext 是一个接口(我想),所以应该有一个具体的类来实现这个接口(例如 AppContext)。这个具体的类必须有一个公共的无参数构造函数。
    • 顺便说一句,我建议您将“AppContext”类重命名为“MyAppContext”之类的名称,因为.NET 已经在 System 命名空间中包含了一个静态 AppContext。
    • 您好 Bigabdoul,我查看了您的答案,但无法正常工作。我创建了一个新的简单项目来说明我的问题。请看这里link。可以看到,你必须有一个无参构造函数,所以我不能不直接使用DI。
    【解决方案2】:

    要从任何类中的 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();
        }
    }
    

    参考:

    Getting value from appsettings.json in .net core

    Read appsettings.json from a class in .NET Core 2

    【讨论】:

    • 我没有使用 Views,它是一个简单的 API。我创建了一个简单的项目来说明我的问题:link。基于此,我假设我必须有一个无参数的构造函数,因此不能有 DI
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-06
    • 1970-01-01
    相关资源
    最近更新 更多