【转载】ASP.NET Core读取appsettings.json配置文件信息

原文地址:https://www.cnblogs.com/zhouxiaoyun/p/10769491.html

1、在配置文件appsettings.json里新增AppSettings节点

复制代码
{
  "Logging": {
    "LogLevel": {
      "Default": "Warning"
    }
  },
  "AppSettings": {
    "HttpUrl": "http://www.ehongcn.com",
    "Copyright": "山南远宏科技有限公司"
  },
  "AllowedHosts": "*"
}
复制代码

2、新建实体类AppSettings,通常建在公共类库Common里

    public class AppSettings
    {
        public string HttpUrl { get; set; }
        public string Copyright { get; set; }
    }

3、在Startup类里的ConfigureServices配置

services.Configure<AppSettings>(Configuration.GetSection("AppSettings"));

4、控制器或者业务类里使用

复制代码
        private readonly AppSettings _appSettings;

        public HomeController(IOptions<AppSettings> appSettings)
        {
            _appSettings = appSettings.Value;
        }

        public IActionResult Index()
        {
            ViewData["Url"] = _appSettings.HttpUrl;
            return View();
        }
复制代码
posted @ 2020-11-11 15:12  吃辣椒的小毛驴  阅读(166)  评论(0编辑  收藏  举报