簡體   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