zoukankan      html  css  js  c++  java
  • appsettings.json

    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 

  • 相关阅读:
    Uva11235
    Uva11300/BZOJ1045/BZOJ1465/BZOJ3292
    树形背包模板
    Miller-Rabin素性测试
    BZOJ1500 : [NOI2005]维修数列-fhq_Treap
    COGS2421 [HZOI 2016]简单的Treap
    用 fhq_Treap 实现可持久化平衡树
    hdu 1010(DFS) 骨头的诱惑
    hdu 1026(BFS+输出路径) 我要和怪兽决斗
    hdu 1072(BFS) 有炸弹
  • 原文地址:https://www.cnblogs.com/Leo_wl/p/5747128.html
Copyright © 2011-2022 走看看