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. Linux-——grep

    概念介绍 grep (global search regular expression(RE) and print out the line,全面搜索正则表达式并把行打印出来)是一种强大的文本搜索工具 ...

  2. Vmware迁移以后eth0消失,无法上网

    一个再普通不过的大神帮助小菜做虚拟机镜像的事情: 小张:帮我做个Vmware下的Ubuntu镜像吧,大神. 小黄:好啊,等我一下,下午发给你. 经过一番操作,小黄顺利的做出了一个虚拟机操作系统 小黄: ...

  3. windows常用运行命令总结

    开始→运行→命令集锦 winver---------检查Windows版本 wmimgmt.msc----打开windows管理体系结构(WMI) wupdmgr--------windows更新程序 ...

  4. Oracle增加一列、修改一列数据类型

    Oracle增加一列.修改一列数据类型: 添加一列: alter   table   A   add( CFYJSNR  varchar2(20)); 修改列: alter  table A  ren ...

  5. [freeCodeCamp] solution to JUGGLING ASYNC

    Here's the official solution in case you want to compare notes: var http = require('http') var bl = ...

  6. 【原型实战】分分钟搞定Unsplash网站原型设计

    网站原型设计是我们在设计网页过程中必不可少的一步,激烈的市场竞争让我们不得不对产品进行快速迭代,如何高速有效的进行原型设计成为了设计师头疼的问题.本文将以unsplash网站为实例,教大家快速搞定we ...

  7. 判定一个数num是否为x的幂

    那个数所在类型中,x的幂最大值为max 1.则第一条判定:max%num==0: 若x不为任何数的幂,则第一条判定足矣. 若x为某个数的幂,则要加判定条件 2.(num-1)%(x-1)==0 同时满 ...

  8. jquery报.live() is not a function的解决方法

    jquery报.live() is not a function的解决方法: jquery中的live()方法在jquery1.9及以上的版本中已被废弃了,如果使用,会抛出TypeError: $(. ...

  9. 2018.10.16 NOIP模拟 膜法(组合数学)

    传送门 原题,原题,全TM原题. 不得不说天天考原题. 其实这题我上个月做过类似的啊,加上dzyodzyodzyo之前有讲过考试直接切了. 要求的其实就是∑i=lr(ii−l+k)\sum _{i=l ...

  10. 2018.09.29 bzoj3156: 防御准备(斜率优化dp)

    传送门 斜率dp经典题目. 然而算斜率的时候并没有注意到下标的平方会爆int于是咕咕*2. 这道题我用了两个数组来表示状态. f[i]f[i]f[i]表示最后i个位置倒数第i个放木偶的最优值. g[i ...