zoukankan      html  css  js  c++  java
  • 04. Asp.Net Core 3.x 笔记 配置文件

    配置文件优先级

    1.appsettings.json
    2.appsettings.{xxxx}.json,比如: appsettings.Development.json
    3.环境变量
    4.命令行

    越靠后,优先级越高,将覆盖前者

    添加自定义配置

    appsettings.json:

    {
      "Logging": {
        "LogLevel": {
          "Default": "Information",
          "Microsoft": "Warning",
          "Microsoft.Hosting.Lifetime": "Information"
        }
      },
      "AllowedHosts": "*",
    
        "Three": {
        "BoldDepartmentEmployeeCountThreshold": 30
      }
    
    }
    

    获取及其使用配置项:

    • Startup.cs
        public class Startup
        {
            private readonly IConfiguration configuration;
            public Startup(IConfiguration configuatuion)
            {
                configuration = configuatuion;
                var three = configuration["Three:BoldDepartmentEmployeeCountThreshold"];
            }
    

    自定义配置对象及其获取

    • 自定义配置对象 ThreeOptions
        public class ThreeOptions
        {
            public int BoldDepartmentEmployeeCountThreshold { get; set; }
            public int MyProperty { get; set; }
        }
    
    • 注册配置对象
        public class Startup
        {
            private readonly IConfiguration configuration;
            public Startup(IConfiguration configuatuion)
            {
                configuration = configuatuion;
                //var three = configuration["Three:BoldDepartmentEmployeeCountThreshold"];
            }
    
            //注册服务
            public void ConfigureServices(IServiceCollection services)
            {
                ...
                services.Configure<ThreeOptions>(configuration.GetSection("Three"));
            }
    
    • xxxControler中使用:
        public class DepartmentController : Controller
        {
            private readonly IOptions<ThreeOptions> threeOptions;
    
            public DepartmentController(IDepartmentService departmentService, IOptions<ThreeOptions> threeOption)
            {
                this.departmentService = departmentService;
            }
    
    • 在Views使用xxx.cshtml中使用:
    @model Three.Models.Department
    @inject Microsoft.Extensions.Options.IOptions<Three.ThreeOptions> options
    ...
            @if (Model.EmployeeCount > options.Value.BoldDepartmentEmployeeCountThreshold)
            {
    ...
            }
    ...
    
    

    自定义配置文件

    • myconfig.json
    {
      "Three": {
        "BoldDepartmentEmployeeCountThreshold": 1000
      }
    }
    

    Program.cs

    
            public static IHostBuilder CreateHostBuilder(string[] args) =>
                Host.CreateDefaultBuilder(args)
                    .ConfigureAppConfiguration((context, configBuilder) => 
                    {
                        //configBuilder.Sources.Clear();
                        configBuilder.AddJsonFile("myconfig.json");
                    })
                    .ConfigureWebHostDefaults(webBuilder =>
                    {
                        webBuilder.UseStartup<Startup>();
                    });
    

    myconfig.json 会覆盖 appsettings.json 相同的配置项

  • 相关阅读:
    s4-9 二层设备
    s4-9 二层设备
    s5-1 网络层引言
    LeetCode Factorial Trailing Zeroes (阶乘后缀零)
    UVA 658 It's not a Bug, it's a Feature! (最短路,经典)
    UVA 1151 Buy or Build (MST最小生成树,kruscal,变形)
    LeetCode Reverse Linked List (反置链表)
    LeetCode Contains Duplicate (判断重复元素)
    UVA 1395 Slim Span (最小生成树,MST,kruscal)
    割点,桥,边双连通分量,点双连通分量
  • 原文地址:https://www.cnblogs.com/easy5weikai/p/13030557.html
Copyright © 2011-2022 走看看