首页 > 移动平台 > 详细

.Net Core 读取 appsettings.json

时间:2018-06-11 18:38:39      阅读:157      评论:0      收藏:0      [点我收藏+]

1. 首先些一个类

public class MySettings
{
    public string P1 { get; set; }
    public string P2 { get; set; }
}

2. 在 appsettings.json 中添加配置项

{
  "Logging": {
    "IncludeScopes": false,
    "LogLevel": {
      "Default": "Warning"
    }
  },
  "MySettings": {
    "P1": "p1_value",
    "P2": "p2_value"
  }
}

3. 修改 Startup.cs

public void ConfigureServices(IServiceCollection services)
{
    services.Configure<MySettings>(Configuration.GetSection("MySettings"));
    services.AddMvc();
}

4.  修改 HomeController.cs

public class HomeController : Controller
{
    private MySettings mySettings { get; set; }

    public HomeController(IOptions<MySettings> mySettings)
    {
        this.mySettings = mySettings.Value;
    }

    public IActionResult Index()
    {
        string p1 = mySettings.P1; 
        return View();
    }
}

 

  

  

 

.Net Core 读取 appsettings.json

原文:https://www.cnblogs.com/anjou/p/9168404.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!