appsettings.json

目录索引 

【无私分享:ASP.NET CORE 项目实战】目录索引

简介

  在我们之前的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的更多相关文章

  1. 【无私分享:ASP.NET CORE 项目实战(第六章)】读取配置文件(一) appsettings.json

    目录索引 [无私分享:ASP.NET CORE 项目实战]目录索引 简介 在我们之前的Asp.net mvc 开发中,一提到配置文件,我们不由的想到 web.config 和 app.config,在 ...

  2. .Net Core 读取appsettings.json的配置

    在.net core中是没有*.config 文件的 配置文件都是*.json 1.在project.json里下面这行代码 "Microsoft.Extensions.Options.Co ...

  3. Talking appsettings.json in Asp.Net Core

    在ASP.NET Core中,默认提供了三个运行时环境变量,通过查看Hosting源代码我们可以看到,分别是Development.Staging.Production public static c ...

  4. 每天记录一点:NetCore获得配置文件 appsettings.json

    用NetCore做项目如果用EF  ORM在网上有很多的配置连接字符串,读取以及使用方法 由于很多朋友用的其他ORM如SqlSugar,NH,Dapper等,在读取连接字符串的时候,往往把信息保存到一 ...

  5. Asp .Net Core 读取appsettings.json配置文件

         Asp .Net Core 如何读取appsettings.json配置文件?最近也有学习到如何读取配置文件的,主要是通过 IConfiguration,以及在Program中初始化完成的. ...

  6. DotNetCore跨平台~在appsettings.json里自定义配置项

    回到目录 DotNetCore里一切都是依赖注入的,对于appsettings这个可扩展的配置对象也不例外,它位于项目根目录,一般在startup里去注册它,在类中通过构造方法注入来获取当前的对象,以 ...

  7. DotNetCore跨平台~关于appsettings.json里各种配置项的读取

    回到目录 对于dotnet Core来说,依赖注入的集成无疑是最大的亮点,它主要用在服务注册与注入和配置文件注册与注入上面,我们一般会在程序入口先注册服务或者文件,然后在需要的地方使用注入即可,下面主 ...

  8. .Net Core 实践 - 如何在控制台应用(.Net Core)使用appsettings.json配置

    新建控制台应用(.Net Core)程序 添加json文件,命名为appsettings.json,设置文件属性 如果较新则复制.添加内容如下 { "MyWords" : &quo ...

  9. 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 ...

随机推荐

  1. [UVA] 784 - Maze Exploration

      Maze Exploration  A maze of rectangular rooms is represented on a two dimensional grid as illustra ...

  2. LeetCode_Rotate Image

    You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwise). ...

  3. Python 的开发环境

    建议在Windows 下开发,成本低廉,简单,效率高. 综合下:开发的程序,Python  Django (Mysql,PostgreSQL) Nginx Redis ,这一组组合可以适应不同的平台, ...

  4. InnoSetup中枚举出INI文件的所有sections和键值

    原文 http://blog.chinaunix.net/uid-23480430-id-3016899.html InnoSetup支持一些INI文件操作函数, 例如GetIniString,Ini ...

  5. c#提出中文首字母

           ; i < len; i++)             {                 myStr += getSpell(strText.Substring(i, ));   ...

  6. (转载)关于#pragma pack(push,1)和#pragma pack(1)

    转载http://www.rosoo.net/a/201203/15889.html 一.#pragma pack(push,1)与#pragma pack(1)的区别 这是给编译器用的参数设置,有关 ...

  7. <转载>C++命名空间

    原文http://blog.csdn.net/liufei_learning/article/details/5391334 一. 为什么需要命名空间(问题提出) 命名空间是ANSIC++引入的可以由 ...

  8. c++智能指针《二》 std::tr1::shared_ptr

    转载http://www.cnblogs.com/kadinzhu/archive/2011/12/12/2284826.html 看<effective c++>,作者一直强调用std: ...

  9. php5.3 appache phpstudy win7win8win10下 运行速度慢

    php5.3 appache phpstudy win7win8win10下 运行速度慢 最近在部署服务器以及本地测试的时候发现了一个奇怪的现象,运行PHP程序的时候非常慢,起先以为是网速的原因,后经 ...

  10. JQuery DOM 有关代码练习

    //累加你选择的个数 <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <hea ...