相关疑难解决方法(0)

从.net核心中的appsettings.json获取价值

不知道我在这里缺少什么,但我无法从我的.net核心应用程序中的appsettings.json获取值.我的appsettings.json为:

{
    "AppSettings": {
        "Version": "One"
    }
}
Run Code Online (Sandbox Code Playgroud)

启动:

public class Startup
{
    private IConfigurationRoot _configuration;
    public Startup(IHostingEnvironment env)
    {
        _configuration = new ConfigurationBuilder()
    }
    public void ConfigureServices(IServiceCollection services)
    {
      //Here I setup to read appsettings        
      services.Configure<AppSettings>(_configuration.GetSection("AppSettings"));
    }
}
Run Code Online (Sandbox Code Playgroud)

模型:

public class AppSettings
{
    public string Version{ get; set; }
}
Run Code Online (Sandbox Code Playgroud)

控制器:

public class HomeController : Controller
{
    private readonly AppSettings _mySettings;

    public HomeController(IOptions<AppSettings> settings)
    {
        //This is always null
        _mySettings = settings.Value;
    }
}
Run Code Online (Sandbox Code Playgroud)

_mySettings永远是空的.这里有什么我想念的吗?

c# asp.net-core-mvc asp.net-core

124
推荐指数
9
解决办法
16万
查看次数

标签 统计

asp.net-core ×1

asp.net-core-mvc ×1

c# ×1