appsettings.json
appsettings.json
目录索引
简介
在我们之前的Asp.net mvc 开发中,一提到配置文件,我们不由的想到 web.config 和 app.config,在 core 中,我们看到了很多的变化,新的配置系统显得更加轻量级,具有更好的扩展性,并且支持多样化的数据源。
博客园对于这个的讲解很多,比如:Artche ,但是,没有点基础看老A的博客还是有些吃力的,对于老A介绍的配置,我也是看的一头雾水,在后面的文章中,我会用像我们这些菜鸟容易接受的方式,重新解释一下。
今天,我们以 appsettings.json 为例,读取一些简单的系统配置。
appsettings.json
在 第二章 中,我们在讲到EF上线文时,在 Startup.cs 添加 services.AddDbContext<ApplicationDbContext>(options => options.UseSqlServer(Configuration.GetConnectionString("SqlServerConnection"))); 已经使用到了 appsettings.json
我们添加一些简单的系统配置,来演示一下读取 appsettings.json:
{
"ApplicationInsights": {
"InstrumentationKey": ""
},
"ConnectionStrings": {
"SqlServerConnection": "Server=.;Database=db_wkmvc;User ID=sa_wkmvc;Password=123456;"
},
"Logging": {
"IncludeScopes": false,
"LogLevel": {
"Default": "Debug",
"System": "Information",
"Microsoft": "Information"
}
},
"ApplicationConfiguration": {
//文件上传路径
"FileUpPath": "/upload/",
//是否启用单用户登录
"IsSingleLogin": "True",
//允许上传的文件格式
"AttachExtension": "gif,jpg,jpeg,png,bmp,rar,zip,doc,docx,xls,xlsx,ppt,pptx,txt,flv,apk,mp4,mpg,ts,mpeg,mp3,bak,pdf",
//图片上传最大值KB
"AttachImagesize": 12400
}
}
我们添加一个配置类 ApplicationConfiguration

1 public class ApplicationConfiguration
2 {
3 #region 属性成员
4
5 /// <summary>
6 /// 文件上传路径
7 /// </summary>
8 public string FileUpPath { get; set; }
9 /// <summary>
10 /// 是否启用单用户登录
11 /// </summary>
12 public bool IsSingleLogin { get; set; }
13 /// <summary>
14 /// 允许上传的文件格式
15 /// </summary>
16 public string AttachExtension { get; set; }
17 /// <summary>
18 /// 图片上传最大值KB
19 /// </summary>
20 public int AttachImagesize { get; set; }
21 #endregion
22 }

在 Startup.cs 的 ConfigureServices 添加
services.Configure<ApplicationConfiguration>(Configuration.GetSection("ApplicationConfiguration"));

添加一个领域层 AppConfigurtaionServices
public class AppConfigurtaionServices
{
private readonly IOptions<ApplicationConfiguration> _appConfiguration;
public AppConfigurtaionServices(IOptions<ApplicationConfiguration> appConfiguration)
{
_appConfiguration = appConfiguration;
}
public ApplicationConfiguration AppConfigurations
{
get
{
return _appConfiguration.Value;
}
}
}
添加引用 using Microsoft.Extensions.Options;
我们来测试一下:

测试结果:

希望跟大家一起学习Asp.net Core
appsettings.json的更多相关文章
- 【无私分享:ASP.NET CORE 项目实战(第六章)】读取配置文件(一) appsettings.json
目录索引 [无私分享:ASP.NET CORE 项目实战]目录索引 简介 在我们之前的Asp.net mvc 开发中,一提到配置文件,我们不由的想到 web.config 和 app.config,在 ...
- .Net Core 读取appsettings.json的配置
在.net core中是没有*.config 文件的 配置文件都是*.json 1.在project.json里下面这行代码 "Microsoft.Extensions.Options.Co ...
- Talking appsettings.json in Asp.Net Core
在ASP.NET Core中,默认提供了三个运行时环境变量,通过查看Hosting源代码我们可以看到,分别是Development.Staging.Production public static c ...
- 每天记录一点:NetCore获得配置文件 appsettings.json
用NetCore做项目如果用EF ORM在网上有很多的配置连接字符串,读取以及使用方法 由于很多朋友用的其他ORM如SqlSugar,NH,Dapper等,在读取连接字符串的时候,往往把信息保存到一 ...
- Asp .Net Core 读取appsettings.json配置文件
Asp .Net Core 如何读取appsettings.json配置文件?最近也有学习到如何读取配置文件的,主要是通过 IConfiguration,以及在Program中初始化完成的. ...
- DotNetCore跨平台~在appsettings.json里自定义配置项
回到目录 DotNetCore里一切都是依赖注入的,对于appsettings这个可扩展的配置对象也不例外,它位于项目根目录,一般在startup里去注册它,在类中通过构造方法注入来获取当前的对象,以 ...
- DotNetCore跨平台~关于appsettings.json里各种配置项的读取
回到目录 对于dotnet Core来说,依赖注入的集成无疑是最大的亮点,它主要用在服务注册与注入和配置文件注册与注入上面,我们一般会在程序入口先注册服务或者文件,然后在需要的地方使用注入即可,下面主 ...
- .Net Core 实践 - 如何在控制台应用(.Net Core)使用appsettings.json配置
新建控制台应用(.Net Core)程序 添加json文件,命名为appsettings.json,设置文件属性 如果较新则复制.添加内容如下 { "MyWords" : &quo ...
- Adding appsettings.json to a .NET Core console app
This is something that strangely doesn’t seem to be that well documented and took me a while to figu ...
随机推荐
- 帮小黎解决问题C++巩固获得数字每个位置上的数
现在有一个数字 a= 12345; 想要取得这个数字上的没一个数字 使用 除法 +模除 的方法可以获得 原理:除(/)得到的是商 模除(%)的到的是余数 采用这种方式,先将要求的数的某一位 ...
- 正则匹配<img src="xxxxxx" alt="" />标签的相关写法
1.(<img\ssrc[^>]*>) 2.content.replace(/<img [^>]*src=['"]([^'"]+)[^>]*&g ...
- N年之后,只记得三井寿!而我们程序猿们也要加油珍惜时间!
[感觉程序员看一篇励志文章效果大于6篇技术文章,3份源码下载.....所以上此文] [说明:本文不少段落是摘自别人文章,因为本人写程序的文笔有限,怕感动不了大家,所以摘取了不错的部分] 前段时间重新看 ...
- HDU 4010 Query on The Trees(动态树)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4010 题意:一棵树,四种操作: (1)若x和y不在一棵树上,将x和y连边: (2)若x和y在一棵树上, ...
- 网络版shell之网络编程练习篇--telnet服务端
网络版shell之网络编程练习篇--telnet服务端 以前写过一个shell命令解释器,对与shell命令解释器的执行流程有了清晰的认识,这段时间学习网络编程,至于网络编程的细节以及知识点,已经 ...
- device tree website
每一个设备都有相对应的初始化程序,dts的写法可以参照Documentations/devicetree/下面的文档 http://bbs.chinaunix.net/thread-4139331-1 ...
- Search for a Range 解答
Question Given a sorted array of integers, find the starting and ending position of a given target v ...
- 沙湖王 | 用Scipy实现K-means聚类算法
沙湖王 | 用Scipy实现K-means聚类算法 用Scipy实现K-means聚类算法
- VS 2012 显示Link的参数
VC 通过Link将cl编译出来的.obj文件链接到一起.不过默认设置还是看不到究竟是怎么做的.需要如下设置: 右键点击工程,选择Properties菜单,然后选择左边的Linker->Gene ...
- Ubuntu 启动器/快捷方式/ 制作 (Eclipse为例)
首先,在路径/usr/share/applications/,中创建eclipse.desktop(如果没有的话) sudo touch /usr/share/applications/eclipse ...