zoukankan      html  css  js  c++  java
  • .netcore读取配置文件

    setting.json

    {
      "compilerOptions": {
        "noImplicitAny": false,
        "noEmitOnError": true,
        "removeComments": false,
        "sourceMap": true,
        "target": "es5"
      },
      "exclude": [
        "node_modules",
        "wwwroot"
      ]
    }

    引用Nuget:
    Microsoft.Extensions.ConfigurationMicrosoft.Extensions.Configuration.Json

            static void Main(string[] args)
            {
                var builder = new ConfigurationBuilder()
                    .SetBasePath(Directory.GetCurrentDirectory()) //SetBasePath设置配置文件所在路径
                    .AddJsonFile("setting.json");
    
                var configRoot = builder.Build();
    
                var compilerOptionsvalue =configRoot.GetSection("compilerOptions").GetSection("noImplicitAny").Value;
                Console.WriteLine(compilerOptionsvalue); 
                //False
                
                var excludevalue = configRoot.GetSection("exclude").GetChildren();
                foreach (var item in excludevalue)
                {
                    Console.WriteLine(item.Value);
                }
                //"node_modules","wwwroot"
    
                Console.Read();
            }

    参考:https://docs.microsoft.com/zh-cn/aspnet/core/fundamentals/configuration/index?view=aspnetcore-2.1

    简单方式

    json

    {
      "Logging": {
        "LogLevel": {
          "Default": "Warning"
        }
      },
      "AllowedHosts": "*",
      "compilerOptions": {
        "noImplicitAny": false,
        "noEmitOnError": true,
        "removeComments": false,
        "sourceMap": true,
        "target": "es5"
      },
      "exclude": [
        "node_modules",
        "wwwroot"
      ],
      "wizerd": [
        { "Name": "tangsan","Age":30  },
        { "Name": "niuchacha","Age":22 }
      ]
    }
    

    读取方式:

                Console.WriteLine($"Default = {this.Configuration["Logging:LogLevel:Default"]}");
                Console.WriteLine($"AllowedHosts = {this.Configuration["AllowedHosts"]}");
                Console.WriteLine($"noImplicitAny = {this.Configuration["compilerOptions:noImplicitAny"]}");
                Console.WriteLine($"target = {this.Configuration["compilerOptions:target"]}");
                Console.WriteLine($"exclude[0] = {this.Configuration["exclude:0"]}");
                Console.WriteLine($"exclude[1] = {this.Configuration["exclude:1"]}");
                Console.WriteLine($"wizerd[0].Name = {this.Configuration["wizerd:0:Name"]}");
                Console.WriteLine($"wizerd[1].Name = {this.Configuration["wizerd:1:Name"]}");

     
    分类: C#, ASP.NET Core
     
  • 相关阅读:
    HTML表单
    CSS等高布局的6种方式
    HTML用户反馈表单
    HTML美化修饰<A>
    sql查询语句 --------一个表中的字段对应另外一个表中的两个字段的查询语句
    jq 表格添加删除行
    js 静止f1到f12 和屏蔽鼠标右键
    手机自适应页面的meta标签
    tp3.2 的验证码的使用
    php多线程抓取网页
  • 原文地址:https://www.cnblogs.com/webenh/p/11434847.html
Copyright © 2011-2022 走看看