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 ...
随机推荐
- Linux iostat监测IO状态(转)
Linux iostat监测IO状态 2010-03-1 | 13:13分类:Linux,技术细节 | 标签:Linux | 53,945 views Linux系统出现了性能问题,一般我 ...
- java设计模式--结构型模式--外观模式
外观模式 概述 为子系统中的一组接口提供一个一致的界面,Facade模式定义了一个高层接口,这个接口使得这一子系统更加容易使用. 适用性 1.当你要为一个复杂子系统提供一个简单接口时.子系统往往因为不 ...
- 初识lucene
lucene的介绍网上有好多,再写一遍可能有点多余了. 使用lucene之前,有一系列的疑问 为什么lucene就比数据库快? 倒排索引是什么,他是怎么做到的 lucene的数据结构是什么样的,cpu ...
- cf446A DZY Loves Sequences
A. DZY Loves Sequences time limit per test 1 second memory limit per test 256 megabytes input standa ...
- YUM配置
一.yum环境的本地源搭建(基于VSFTP): 1)安装vsftp; @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ [root ...
- Redis 该选择哪种持久化配置
这个标题或许会让你想起<黑客帝国>里经典的台词,你要选择蓝色药丸,还是红色药丸? Redis 是我们重度使用的一个开源软件,对它的持久化配置做一番相对深入的总结,是值得的.目前它有两种主流 ...
- 2D和3D空间中计算两点之间的距离
自己在做游戏的忘记了Unity帮我们提供计算两点之间的距离,在百度搜索了下. 原来有一个公式自己就写了一个方法O(∩_∩)O~,到僵尸到达某一个点之后就向另一个奔跑过去 /// <summary ...
- Python 获取Facebook用户Friends的爱好类别中的Top10
CODE: #!/usr/bin/python # -*- coding: utf-8 -*- ''' Created on 2014-8-12 @author: guaguastd @name: f ...
- Unity目录结构
http://www.cnblogs.com/liudq/p/5540051.htmlUnity中有几个默认目录 Unity5.x Resources 项目中默认的资源路径,会直接打包到游戏包中.即使 ...
- 打印log 保存log
using UnityEngine; using System.Collections; using System.IO; using System; using System.Text; names ...