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. [leetcode]257. Binary Tree Paths二叉树路径

    Given a binary tree, return all root-to-leaf paths. Note: A leaf is a node with no children. Example ...

  2. C# 类初始化顺序

    C#的类初始化顺序和Java以及C++类初始化顺序是不同的,曾经我被这个问题害惨了.对于C#和Java,其共同点都是先变量后构造函数,先静态后普通 区别在于,C#是子类变量->父类变量-> ...

  3. MVC中构建Linq条件、排序、Selector字段过滤

    代码: System.Linq.Expressions.Expression<Func<Domain.S_ROLE, bool>> expressWhere1 = (c =&g ...

  4. Linux dkpg命令

    一.简介 dpkg 是Debian Package 的简写,是Debian系列系统下的一个软件安装.更新及移除工具. 二.常用指令 1.查询功能 查看软件包信息: dpkg -info xxx.deb ...

  5. 06 Maven 聚合和继承

    Maven 聚合和继承 1. 聚合 2. 继承 <parent> <groupId>org.apache.karaf.demos</groupId> <art ...

  6. 如何从dvi生成pdf--------亲测有效果.

    用里面第二个命令. http://blog.csdn.net/u014682350/article/details/46482477

  7. 学习前端的菜鸡对JS的call,apply,bind的通俗易懂理解

       call,apply,bind 在JavaScript中,call.apply和bind是Function对象自带的三个方法,都是为了改变函数体内部 this 的指向.            a ...

  8. java中的实例化

    java中的new用于实例化一个对象 T1 a= new T1(); T2 b= new T1(); 区别: 问题1:不是实例化一个a,是实例化一个T1 T1 的一个 对象的引用 a 指向了堆空间里的 ...

  9. python3中 for line1 in f1.readlines():,for line1 in f1:,循环读取一个文件夹

    循环读取一个文件: fr.seek(0) fr.seek(0, 0) 概述 seek() 方法用于移动文件读取指针到指定位置. 语法 seek() 方法语法如下: fileObject.seek(of ...

  10. Get异步请求

    1: 使用代理 //1.delegate         //创建url      NSURL *url = [[NSURL alloc] initWithString:@"http:ima ...