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文件的更多相关文章

  1. 解决.net core读取appSetting.json文件中文字符乱码

    如上所诉 vs菜单栏中  :工具 =>自定义 => 命令 =>添加命令 =>文件 =>找到高级保存选项点击 然后关闭,这时在visual studio界面就会有高级保存选 ...

  2. asp.net core读取appsettings.json,如何读取多环境开发配置

    摘要 在读取appsettings.json文件中配置的时候,觉得最简单的方式就是使用asp.net core注入的方式进行读取了. 步骤 首先根据配置项的结构定义一个配置类,比如叫AppSettin ...

  3. .net core 读取appsetting.json

    1.在appsetting.json 文件中添加自定义配置 { "Logging": { "LogLevel": { "Default": ...

  4. Asp .Net Core 读取appsettings.json配置文件

         Asp .Net Core 如何读取appsettings.json配置文件?最近也有学习到如何读取配置文件的,主要是通过 IConfiguration,以及在Program中初始化完成的. ...

  5. asp.net core 读取appsettings.json配置项

    1.新建一个asp.net core 项目 2.打开appsettings.json,加入配置项 { "Logging": { "IncludeScopes": ...

  6. ASP.NET CORE读取appsettings.json的配置

    如何在appsettings.json配置应用程序设置,微软给出的方法:https://docs.microsoft.com/en-us/aspnet/core/fundamentals/config ...

  7. asp.net core 读取Appsettings.json 配置文件

    Appsettingsjson 配置定义实体在StartUp时读取配置信息修改你的Controller通过构造函数进入配置信息总结Appsettings.json 配置很明显这个配置文件就是一个jso ...

  8. ASP.NET Core读取appsettings.json配置文件信息

    1.在配置文件appsettings.json里新增AppSettings节点 { "Logging": { "LogLevel": { "Defau ...

  9. .net core 读取appsettings.json 文件中文乱码的问题

    解决办法:设置高级保存选项 第一步:在工具栏找到自定义选项 第二步:添加高级保存选项Advanced save options 第三步:在Appsettings.json页面操作

随机推荐

  1. Connecting to MQSeries with .NET

    By connecting to MQSeries withing a .NET application, first it has to be done is to install MQ Serie ...

  2. DICOM

    DICOM(Digital Imaging and Communications in Medicine)即医学数字成像和通信,是医学图像和相关信息的国际标准(ISO 12052).它定义了质量能满足 ...

  3. axios 设置拦截器 全局设置带默认参数(发送 token 等)

    应用场景: 1,每个请求都带上的参数,比如token,时间戳等. 2,对返回的状态进行判断,比如token是否过期 代码如下: [javascript] view plain copy axios.i ...

  4. JAVA对字符串的压缩与解压缩

    import java.io.ByteArrayInputStream;import java.io.ByteArrayOutputStream;import java.io.IOException; ...

  5. 网段;IP;广播地址;子网掩码;

    网段(network segment)一般指一个计算机网络中使用同一物理层设备(传输介质,中继器,集线器等)能够直接通讯的那一部分.例如,从192.168.0.1到192.168.255.255这之间 ...

  6. 2018.09.27 网络协议(tarjan)

    描述 一些学校连接在一个计算机网络上.学校之间存在软件支援协议.每个学校都有它应支援的学校名单(学校 a 支援学校 b ,并不表示学校 b 一定支援学校 a ).当某校获得一个新软件时,无论是直接得到 ...

  7. hdu-1181(bfs)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1181 思路:bfs,就是每次找到匹配麻烦一点,注意如果结尾和开头相同,就不算. #include< ...

  8. C语言程序设计50例(一)(经典收藏)

    [程序1]题目:有1.2.3.4个数字,能组成多少个互不相同且无重复数字的三位数?都是多少?1.程序分析:可填在百位.十位.个位的数字都是1.2.3.4.组成所有的排列后再去 掉不满足条件的排列. # ...

  9. UVa 11134 Fabled Rooks (贪心+问题分解)

    题意:在一个n*n的棋盘上放n个车,让它们不互相攻击,并且第i辆车在给定的小矩形内. 析:说实话,一看这个题真是没思路,后来看了分析,原来这个列和行是没有任何关系的,我们可以分开看, 把它变成两个一维 ...

  10. 在linux系统中安装VSCode(Visual Studio Code)和图标的创建方式

    本文转载自:https://www.cnblogs.com/lzpong/p/6145511.html,自己添加了一些关于依赖包安装的. 1.从官网下载压缩包(话说下载下来解压就直接可以运行了咧,都不 ...