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页面操作
随机推荐
- swift 官方文档
swift 官方文档 https://swift.org/blog/
- php 中的信号处理
首先我们需要了解几个函数 pcntl_signal 安装信号处理器,也就是当指定信号发生时,调用函数. pcntl_alarm 指定秒数后向进程发送SIGALRM信号. posix_getpi ...
- FormValidator表单验证
所需的库文件 红色框内是所需要的JavaScript库文件. <%@ page language="java" contentType="text/html; ch ...
- Js下载文件到本地(兼容多浏览器)
在客户端通过js下载文件,试过几种下载方式,iframe方式仅限于IE浏览器,window.open(url),location.href=url 这两种方式在chrome浏览器还会是直接打开文件而不 ...
- IPEndPoint
.NET框架用IPEndPoint 对象来表示一个特定的IP地址和端口的组合,应用该对象的场景多是在讲socket绑定到本地地址或者将socket绑定到非本地地址.
- 设计社区类Web原型制作分享-Behance
Behance 是著名设计社区,创意设计人士可以展示自己的作品,发现别人分享的创意作品. 网站有二级导航,主要用到的交互组件有弹出面板,通过弹出面板来隐藏展现搜索框.并且用到的组件有播放器.菜单栏.下 ...
- Python鸭子类型思想
动态语言中经常提到鸭子类型,所谓鸭子类型就是:如果走起路来像鸭子,叫起来也像鸭子,那么它就是鸭子(If it walks like a duck and quacks like a duck, it ...
- 2018.09.18 atcoder Many Formulas(搜索)
传送门 感觉自己搜索能力退化了,这种弱智搜索写了整整5min,这样下去比赛会凉的. 看来得多练练题了. 代码: #include<bits/stdc++.h> #define ll lon ...
- cgo -rpath指定动态库路径
// #cgo CFLAGS: -Wall // #cgo LDFLAGS: -Wl,-rpath="/home/liuliang/ffmpeg-build/lib" // #cg ...
- 机器学习 数据预处理之独热编码(One-Hot Encoding)
问题由来 在很多机器学习任务中,特征并不总是连续值,而有可能是分类值. 例如,考虑一下的三个特征: ["male", "female"] ["from ...