ASP.NET core Web中使用appsettings.json配置文件的方法
程序员文章站
2023-11-25 13:04:22
前言
最近在研究把asp.net程序移植到linux上,正好.net core出来了,就进行了学习。
移植代码基本顺利,但是发现.net core中没有configur...
前言
最近在研究把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(); } }
总结
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作能带来一定的帮助,如果有疑问大家可以留言交流,谢谢大家对的支持。
上一篇: python删除指定类型(或非指定)的文件实例详解
下一篇: 微信开发 微信授权详解
推荐阅读
-
ASP.Net Core中使用枚举类而不是枚举的方法
-
在Django中同时使用多个配置文件的方法
-
ASP.NET core Web中使用appsettings.json配置文件的方法
-
ASP.NET Core中预压缩静态文件的方法步骤
-
灵活掌握Asp.net MVC中GridView的使用方法
-
ASP.NET MVC4中使用Html.DropDownListFor的方法示例
-
ASP.NET Core静态文件的使用方法
-
web.py在SAE中的Session问题解决方法(使用mysql存储)
-
ASP.NET Core 3.0 : 二十八. 在Docker中的部署以及docker-compose的使用
-
ASP.Net Core中使用枚举类而不是枚举的方法