.net core 读取、修改配置文件appsettings.json
程序员文章站
2022-07-10 23:43:12
.net core 设置读取JSON配置文件 appsettings.json Startup.cs 中 类库中 使用 这种方法可以保证可以修改正在运行的dotnet core 程序,不需要重启程序 使用Newtonsoft.Json 查询/修改,以及修改 appsettings.json 文件 R ......
.net core 设置读取json配置文件 appsettings.json
startup.cs 中
public class startup { public startup(ihostingenvironment env) { configuration = new configurationbuilder() .setbasepath(env.contentrootpath) .addjsonfile("appsettings.json", optional: true, reloadonchange: true) .addjsonfile($"appsettings.{env.environmentname}.json", optional: true, reloadonchange: true) //增加环境配置文件,新建项目默认有 .addenvironmentvariables() .build(); } public iconfiguration configuration { get; } ....
类库中
var builder = new configurationbuilder() .setbasepath(directory.getcurrentdirectory()) .addjsonfile("appsettings.json") .addjsonfile("appsettings.test.json", true, reloadonchange: true); var config = builder.build(); //读取配置 var a = config["jwtsettings:secret"];
使用.addjsonfile("appsettings.json", optional: true, reloadonchange: true)
这种方法可以保证可以修改正在运行的dotnet core 程序,不需要重启程序
使用newtonsoft.json 查询/修改,以及修改 appsettings.json 文件
readobject
public iactionresult readobject() { string json = @"{ 'cpu': 'intel', 'psu': '500w', 'drives': [ 'dvd read/writer' /*(broken)*/, '500 gigabyte hard drive', '200 gigabyte hard drive' ] }"; jsontextreader reader = new jsontextreader(new stringreader(json)); var string1 = ""; while (reader.read()) { if (reader.value != null) { string1 += "reader.value != null: token: " + reader.tokentype + ", value: " + reader.value + " <br/>"; } else { string1 += "reader.value == null: token: " + reader.tokentype + " <br/>"; } } viewdata["string"] = string1; return view(); }
readjson 读取json 文件
newtonsoft.json
使用newtonsoft.json效果如截图
public class homecontroller : controller { private readonly ihostingenvironment _hostingenvironment; public homecontroller(ihostingenvironment hostingenvironment) { _hostingenvironment = hostingenvironment; } public iactionresult readjson() { string contentpath = _hostingenvironment.contentrootpath + @"\"; ; //项目根目录 var filepath = contentpath + "appsettings.json"; using (streamreader file = new streamreader(filepath)) using (jsontextreader reader = new jsontextreader(file)) { jobject o2 = (jobject)jtoken.readfrom(reader); string licencekey = (string)o2["appsettings"]["key"]; viewdata["string"] = licencekey; return view(); } }
microsoft.extensions.configuration
使用microsoft.extensions.configuration
public iactionresult index() { var builder = new configurationbuilder() .setbasepath(directory.getcurrentdirectory()) .addjsonfile("appsettings.json") .addjsonfile("appsettings.test.json", true, reloadonchange: true); var config = builder.build(); //读取配置 viewdata["secret"] = config["appsettings:key"]; return view(); }
修改 appsettings.json后的效果
newtonsoft.json
microsoft.extensions.configuration
修改 appsettings.json
streamwriter直接覆盖
public class homecontroller : controller { private readonly ihostingenvironment _hostingenvironment; public homecontroller(ihostingenvironment hostingenvironment) { _hostingenvironment = hostingenvironment; } public iactionresult writejson() { string contentpath = _hostingenvironment.contentrootpath + @"\"; ; //项目根目录 var filepath = contentpath + "appsettings.json"; jobject jsonobject; using (streamreader file = new streamreader(filepath)) using (jsontextreader reader = new jsontextreader(file)) { jsonobject = (jobject)jtoken.readfrom(reader); jsonobject["appsettings"]["key"] = "asdasdasdasd"; } using (var writer = new streamwriter(filepath)) using (jsontextwriter jsonwriter = new jsontextwriter(writer)) { jsonobject.writeto(jsonwriter); } return view(); }
效果如图
缺点 格式化的json和注释都没了
推荐阅读
-
详解ASP.NET Core实现强类型Configuration读取配置数据
-
asp.net core 配置文件动态更新
-
.net core 学习 读取配置文件
-
.net core 读取appsettings.json 文件中文乱码的问题
-
干货:.net core实现读取自定义配置文件,有源代码哦
-
干货:.net core实现读取appsettings.json配置文件(建议收藏)
-
我来告诉你:VS2019开发ASP.NET Core 3.0 Web项目,修改视图后,刷新浏览器看不到修改后的效果怎么处理
-
.net core 读取本地指定目录下的文件的实例代码
-
ASP.NET Core自定义本地化教程之从文本文件读取本地化字符串
-
.NET Core 使用NPOI读取Excel返回泛型List集合