我試圖在.Net Core 3.1 MVC 中從appsettings 到MyController 獲取價值。在這里,我需要通過以下方式實作這一點:
private readonly IConfiguration _config;
public MyController(IConfiguration config)
{
_config = config;
}
public string apiURL = _config.GetValue<string>("ServiceURL");
但是當我嘗試這樣做時,我遇到了錯誤。
欄位初始值設定項不能參考非靜態欄位、方法或屬性“MyController._config”
在此處輸入影像描述
uj5u.com熱心網友回復:
將您的欄位改為屬性,如下所示:
public string apiURL => _config.GetValue<string>("ServiceURL");
使用時,=您正在創建一個欄位。
使用時,=>您正在創建一個 get-only 屬性。
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/430019.html
