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 

  • 相关阅读:
    查看端口有没有被占用
    微信公众号2()
    How to insert a segment of noise to music file
    puppet practice
    Docker Commands
    LempelZiv algorithm realization
    The algorithm of entropy realization
    Java network programmingguessing game
    Deploy Openstack with RDO and Change VNC console to Spice
    puppet overview
  • 原文地址:https://www.cnblogs.com/Leo_wl/p/5747128.html
Copyright © 2011-2022 走看看