zoukankan      html  css  js  c++  java
  • 【无私分享:ASP.NET CORE 项目实战(第六章)】读取配置文件(一) appsettings.json

     

    目录索引 

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

     

    简介

      在我们之前的Asp.net mvc 开发中,一提到配置文件,我们不由的想到 web.configapp.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 

    刚开始接触,水平有限,很多东西都是自己的理解和翻阅网上大神的资料,如果有不对的地方和不理解的地方,希望大家指正!

    虽然Asp.net Core 现在很火热,但是网上的很多资料都是前篇一律的复制,所以有很多问题我也暂时没有解决,希望大家能共同帮助一下!

     

    原创文章 转载请尊重劳动成果 http://yuangang.cnblogs.com

     

  • 相关阅读:
    POJ 3258 (NOIP2015 D2T1跳石头)
    POJ 3122 二分
    POJ 3104 二分
    POJ 1995 快速幂
    409. Longest Palindrome
    389. Find the Difference
    381. Insert Delete GetRandom O(1)
    380. Insert Delete GetRandom O(1)
    355. Design Twitter
    347. Top K Frequent Elements (sort map)
  • 原文地址:https://www.cnblogs.com/yuangang/p/5736892.html
Copyright © 2011-2022 走看看