ASP.NET core Web中使用appsettings.json配置文件的方法
发布时间:2021-01-12 03:36:38 所属栏目:asp.Net 来源:互联网
导读:前言最近在研究把asp.net程序移植到linux上,正好.netcore出来了,就进行了学习。
前言 最近在研究把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() { ViewData["SiteName"] = ConfigSettings.SiteName; return View(); } } 总结 以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作能带来一定的帮助,如果有疑问大家可以留言交流,谢谢大家对编程小技巧的支持。 您可能感兴趣的文章:
(编辑:岳阳站长网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- asp.net-mvc – 如何锁定ASP.NET MVC中的路径?
- asp.net-mvc – 如何在asp.net mvc3项目中开始使用openID?
- 点击图片,AJAX删除后台图片文件的实现代码(asp.net)
- asp.net – 适用于多个用户的EWS通知中心
- ASP.NET web.config文件是否失控?
- asp.net-mvc – 使用Viewbag绑定DropdownlistFor
- asp.net – 在将MVC和路由添加到WebForms项目后,IIS中的默认
- VS 2015 CTP 6 Nuget Package Source
- asp.net 大文件上传控件
- 利用ASP.NET MVC和Bootstrap快速搭建个人博客之后台dataTab
推荐文章
站长推荐
- asp.net – 双回发问题
- asp.net – 如何访问Global.asax静态成员?
- asp.net – 如何使用ajax调用跨域web api?
- asp.net – WebForms:MasterPages中的动态(或绝
- asp.net – 访问.NET中的Web服务中的查询字符串(
- asp.net-mvc – 在IIS Express中测试SignalR应用
- asp.net-mvc-3 – 用于在ASP.NET MVC3中使用Grid
- asp.net – 使用FormsAuthentication持久的cooki
- asp.net-mvc-4 – 通过ADAL JavaScript Ajax和Kn
- asp.net汉字转拼音和获取汉字首字母的代码
热点阅读