asp.net core读取appsetting.json文件
1、在Startup.cs文件中注入,ConfigureServices方法
services.Configure<MyConfig>(Configuration.GetSection("MyConfig"));
AppSetting.json文件
"MyConfig": {
"ResVersion": "1.0.2 Beta"
},
自定义类
public class MyConfig
{
public string ResVersion { get; set; }
}
使用
public class ValuesController : Controller
{
private readonly MyConfig _config; public ValuesController(IOptions<MyConfig> op)
{
_config = op.Value;
}
}
2、读取指定节点下节点值。Nuget引入Microsoft.Extensitions.Configuration。
自定义AppSettingHelper.cs。
public class AppSettingsHelper
{
private static IConfigurationSection appSections = null; public static string AppSetting(string key)
{
string str = "";
if (appSections.GetSection(key) != null)
{
str = appSections.GetSection(key).Value;
}
return str;
}
public static void SetAppSetting(IConfigurationSection section)
{
appSections = section;
}
}
在Startup.cs中ConfigureServices引入
AppSettingsHelper.SetAppSetting(Configuration.GetSection("AppSettings"));
AppSetting.json文件,只限AppSettings下一级节点
"AppSettings": {
"Url": "test"
}
使用
var url = AppSettingsHelper.AppSetting("Url");
3、不通过依赖注入形式。自定义AppSettingHelper.cs类。
Nuget引入Microsoft.Extensitions.Configuration和Microsoft.Extensitions.Configuration.Json
public class AppSettingsHelper
{
public static IConfiguration Configuration { get; set; }
static AppSettingsHelper()
{
//ReloadOnChange = true 当appsettings.json被修改时重新加载
Configuration = new ConfigurationBuilder()
.Add(new JsonConfigurationSource { Path = "appsettings.json", ReloadOnChange = true })
.Build();
} public static string SMSSign
{
get { return Configuration["SMS:SMSSign"]; }
}
}
AppSetting.json配置文件
"SMS": {
"SMSSign": "测试"
},
使用直接调用 AppSettingHelper.SMSSign;
asp.net core读取appsetting.json文件的更多相关文章
- 解决.net core读取appSetting.json文件中文字符乱码
如上所诉 vs菜单栏中 :工具 =>自定义 => 命令 =>添加命令 =>文件 =>找到高级保存选项点击 然后关闭,这时在visual studio界面就会有高级保存选 ...
- asp.net core读取appsettings.json,如何读取多环境开发配置
摘要 在读取appsettings.json文件中配置的时候,觉得最简单的方式就是使用asp.net core注入的方式进行读取了. 步骤 首先根据配置项的结构定义一个配置类,比如叫AppSettin ...
- .net core 读取appsetting.json
1.在appsetting.json 文件中添加自定义配置 { "Logging": { "LogLevel": { "Default": ...
- Asp .Net Core 读取appsettings.json配置文件
Asp .Net Core 如何读取appsettings.json配置文件?最近也有学习到如何读取配置文件的,主要是通过 IConfiguration,以及在Program中初始化完成的. ...
- asp.net core 读取appsettings.json配置项
1.新建一个asp.net core 项目 2.打开appsettings.json,加入配置项 { "Logging": { "IncludeScopes": ...
- ASP.NET CORE读取appsettings.json的配置
如何在appsettings.json配置应用程序设置,微软给出的方法:https://docs.microsoft.com/en-us/aspnet/core/fundamentals/config ...
- asp.net core 读取Appsettings.json 配置文件
Appsettingsjson 配置定义实体在StartUp时读取配置信息修改你的Controller通过构造函数进入配置信息总结Appsettings.json 配置很明显这个配置文件就是一个jso ...
- ASP.NET Core读取appsettings.json配置文件信息
1.在配置文件appsettings.json里新增AppSettings节点 { "Logging": { "LogLevel": { "Defau ...
- .net core 读取appsettings.json 文件中文乱码的问题
解决办法:设置高级保存选项 第一步:在工具栏找到自定义选项 第二步:添加高级保存选项Advanced save options 第三步:在Appsettings.json页面操作
随机推荐
- EditTex
<EditText android:layout_width="match_parent" android:layout_height="100dp" a ...
- iframe父窗口和子窗口之间的调用
1>父窗口获取子窗口 js方法 document.getElementById('if1').contentWindow.document: window.frames["if1&qu ...
- sublime Text与python3的中文编码错误解决办法
在 linux服务器上运行代码报错: Python3中遇到UnicodeEncodeError: ‘ascii’ codec can’t encode characters in ordinal no ...
- spring boot2.03 spring cloud Finchley.RELEASE遇到的问题
1.spring cloud bus 本地不能更新 原因是@RefreshScope注解要加在需要更新的controller上 2.No instances found of configserver ...
- PID算法(C语言)
/************ PID算法(C语言) ************/ #include <stdio.h> #include<math.h> struct _pid { ...
- 小X归来 模拟赛1 解析
Problem1 单峰 小X 归来后,首先对数列很感兴趣.他想起有1类特殊的数列叫单峰数列. 我们说一个数列 {ai} 是单峰的,当且仅当存在一个位置 k 使得 ai < ai+1(i < ...
- abort: no username supplied (see "hg help config")
abort: no username supplied (see "hg help config") 在hg中输入commit 指令时,如果出现下述结果: $ hg commit ...
- java.text.SimpleDateFormat的使用
SimpleDateFormat 是一个以国别敏感的方式格式化和分析数据的具体类. 它允许格式化 (date -> text).语法分析 (text -> date)和标准化. Simpl ...
- jQuery使用大全
我的程序人生 提供基于Lesktop的IM二次开发,联系QQ:76159179 CnBlogs Home New Post Contact Admin Rss Posts - 476 Article ...
- 【转载】foreach+Control.Controls无法一次性移除所有子控件解决方法
博客转载地址:http://www.mzwu.com/article.asp?id=2254 //在panel1中添加20个Button ; ; ; i <= ; i++) { ) row++; ...