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 ...
随机推荐
- 行为级和RTL级的区别(转)
转自:http://hi.baidu.com/renmeman/item/5bd83496e3fc816bf14215db RTL级,registertransferlevel,指的是用寄存器这一级别 ...
- hdu 3461 Code Lock
http://acm.hdu.edu.cn/showproblem.php?pid=3461 并差集和幂取模 这道题主要是求不可操作区间. #include <cstdio> #inclu ...
- LeetCode_Recover Binary Search Tree
Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without changing ...
- Android高德地图自定义Markers的例子
下文为各位重点介绍关于Android高德地图自定义Markers的例子,希望这篇文章能够让各位理解到Android高德地图自定义Markers的方法. 之前的博客里说了地图的嵌入和定位,今天就说说在地 ...
- Buffer lock
buffer lock Oracle 提供非常精确,有效的Row Level Lock机制,多个用户同时修改数据时,为了保护数据. 以块为单位挂起锁的情况不会发生,但这不太正确. 以块为单位的锁 ...
- Asp.net MVC 3 防止 Cross-Site Request Forgery (CSRF)原理及扩展 安全 注入
原理:http://blog.csdn.net/cpytiger/article/details/8781457 原文地址:http://www.cnblogs.com/wintersun/archi ...
- Remove Invalid Parentheses 解答
Question Remove the minimum number of invalid parentheses in order to make the input string valid. R ...
- IOS开发错误提示原因集合-----长期更新
"[__NSCFConstantString size]: unrecognized selector sent to instance." =>将NSString类型的参数 ...
- Runtime运行时机制
Runtime 又叫运行时,是一套底层的 C 语言 API,其为 iOS 内部的核心之一,我们平时编写的 OC 代码,底层都是基于它来实现的 我们需要了解的是 Objective-C 是一门动态语言, ...
- java-程序执行原理
java应用可以打包成jar 格式,jar格式其实只是一种很普通的压缩格式,与zip格式一样,只不过是它会在压缩文件的目录结构中增加一个META-INF/ MANIFEST.MF 的元文件. 我们知道 ...