没有合适的资源?快使用搜索试试~ 我知道了~
ASP.NET core Web中使用appsettings.json配置文件的方法
13 下载量 10 浏览量
2020-10-19
23:40:27
上传
评论
收藏 39KB PDF 举报
温馨提示


试读
2页
主要给大家介绍了在ASP.NET core Web中使用appsettings.json配置文件的方法,文中给出了详细的示例代码,需要的朋友可以参考学习,下面来一起看看吧。
资源推荐
资源详情
资源评论




















ASP.NET core Web中使用中使用appsettings.json配置文件的方法配置文件的方法
主要给大家介绍了在ASP.NET core Web中使用appsettings.json配置文件的方法,文中给出了详细的示例代
码,需要的朋友可以参考学习,下面来一起看看吧。
前言前言
最近在研究把asp.net程序移植到linux上,正好.net core出来了,就进行了学习。
移植代码基本顺利,但是发现.net core中没有ConfigurationManager,无法读写配置文件,单独写个xml之类的嫌麻烦,就谷
歌了下,发现了个方法,遂记录如下,方便以后查找:
方法如下方法如下
配置文件结构
public class DemoSettings
{
public string MainDomain { get; set; }
public string SiteName { get; set; }
}
appsettings.json中显示效果
appsettings.json
{
"DemoSettings": {
"MainDomain": "http://www.mysite.com",
"SiteName": "My Main Site"
},
"Logging": {
"IncludeScopes": false,
"LogLevel": {
"Default": "Debug",
"System": "Information",
"Microsoft": "Information"
}
}
}
配置Services
原配置
public void ConfigureServices(IServiceCollection services)
{
// Add framework services.
services.AddMvc();
}
自定义
public void ConfigureServices(IServiceCollection services)
{
// Add framework services.
services.AddMvc();
// Added - uses IOptions<T> for your settings.
services.AddOptions();
// Added - Confirms that we have a home for our DemoSettings
services.Configure<DemoSettings>(Configuration.GetSection("DemoSettings"));
}
然后把设置注入进相应的Controller后就可以使用了
public class HomeController : Controller
{
private DemoSettings ConfigSettings { get; set; }
public HomeController(IOptions<DemoSettings> settings)
{
ConfigSettings = settings.Value;
}
public IActionResult Index()
资源评论


weixin_38660918
- 粉丝: 9
- 资源: 927
上传资源 快速赚钱
我的内容管理 展开
我的资源 快来上传第一个资源
我的收益
登录查看自己的收益我的积分 登录查看自己的积分
我的C币 登录后查看C币余额
我的收藏
我的下载
下载帮助


安全验证
文档复制为VIP权益,开通VIP直接复制
