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页面操作
随机推荐
- [leetcode]636. Exclusive Time of Functions函数独占时间
Given the running logs of n functions that are executed in a nonpreemptive single threaded CPU, find ...
- 20-java 对象链表空没空呢
写了一个 对象链表,往里面add了一些对象,最后我想看下链表是否为空,用 == null 为假,也看不出, 看下长度? 好吧, size() = 1: 打印 null , 那到底是不是空 啊, ...
- jDeveloper运行慢
最近在使用 Jdeveloper 10.1.3.3 版本时发现速度奇慢无比,后经Google,发现如下解决方案:在 jdev.conf 文件的末尾加上如下两行,速度即可得到显著的提高. AddVMOp ...
- 把Linq查询返回的var类型的数据 转换为DataTable EF连接查询
问题:我要获得一个角色下对应的所有用户,需要两表连接查询,虽然返回的只有用户数据,但是我想到若是返回的不只是用户数据,而还要加上角色信息,那么我返回什么类型呢,返回var吗,这样不行. 于是我网上找找 ...
- 注册带有Portal功能的DYN365_ENTERPRISE_PLAN1地址
使用官方进入的注册页面注册后试用,发现没有Portal功能. https://trials.dynamics.com/Dynamics365/Signup 使用以下的地址注册可以产生Portal ht ...
- window下装redis扩展(以php5.5为例)
一.安装redis服务 1.下载并解压 https://github.com/dmajkic/redis/downloads 2.运行redis服务 3.检查能否正常访问 二.安装redis扩展 1 ...
- [BAT]win7下用批处理脚本自动删除7天以前创建的文件
set JmeterPath=D:\apache-jmeter-2.7 forfiles /p %JmeterPath%\extras /m *.html -d -7 /c "cmd /c ...
- LINUX网络编程 IO 复用
参考<linux高性能服务器编程> LINUX下处理多个连接时候,仅仅使用多线程和原始socket函数,效率十分低下 于是就出现了selelct poll epoll等IO复用函数. 这 ...
- 2018.08.19 NOIP模拟 change(简单模拟)
Change 题目背景 SOURCE:NOIP2015-SHY-10 题目描述 Alice 和 Bob 又聚在一起了!他们已经厌倦了取石子游戏,现在他们热衷于切题.于是,Alice 找到了一道题让 B ...
- asp.net Hessian 服务的注册
Hessian服务端实现了IHttpHandle, 默认情况下是在Web.Config中的handles接点中注册,这样当有 很多实现时比较麻烦 这个时候可以实现IHttpHandleFactory注 ...