zoukankan      html  css  js  c++  java
  • Core的学习四:.Net Core读取配置文件(JSON文件)

    appsettings.json

    {
      "Logging": {
        "LogLevel": {
          "Default": "Information",
          "Microsoft": "Warning",
          "Microsoft.Hosting.Lifetime": "Information"
        }
      },
      "option1": "Json",
      "option2": 2,
    
      //对象
      "subsection": {
        "Id": 1,
        "Name": "Max"
      },
    
      //数组
      "wizards": 
        [
          {
            "Name": "Gand",
            "Age": "10"
          },
          {
            "Name": "Harry",
            "Age": "17"
          }
        ],
    
      "AllowedHosts": "*"
    }

    Startup.cs

            public void Configure(IApplicationBuilder app, IWebHostEnvironment env)//,ILoggerFactory factory
            {
                #region Asp.Net Core读取配置文件(JSON文件) 
                {
                    //xml path,不用区分大小写
                    WriteLine($"option1 = {this.Configuration["option1"]}");
                    WriteLine($"option2 = {this.Configuration["option2"]}");
                    //对象获取
                    WriteLine($"subsection_Id = {this.Configuration["subsection:Id"]}");
                    WriteLine($"subsection_Name = {this.Configuration["subsection:Name"]}");
                    //数组获取
                    WriteLine("wizards");
                    WriteLine($"wizardsFirst_Name = {this.Configuration["wizards:0:Name"]}");
                    WriteLine($"wizardsFirst_Age = {this.Configuration["wizards:0:Age"]}");
                    WriteLine($"wizardsSecond_Name = {this.Configuration["wizards:1:Name"]}");
                    WriteLine($"wizardsSecond_Age = {this.Configuration["wizards:1:Age"]}");
                }
                #endregion
                
                if (env.IsDevelopment())
                {
                    app.UseDeveloperExceptionPage();
                }
                else
                {
                    app.UseExceptionHandler("/Home/Error");
                }
                
                app.UseStaticFiles();
                app.UseSession();
                app.UseRouting();
    
                app.UseAuthorization();
    
                app.UseEndpoints(endpoints =>
                {
                    endpoints.MapControllerRoute(
                        name: "default",
                        pattern: "{controller=Home}/{action=Index}/{id?}");
                });
            }
  • 相关阅读:
    对C# .Net4.5异步机制测试
    权限系统设计
    C#基础知识
    eclipse+pyDev
    Ubuntu下使用sublime text进行py开发
    110_02 补充模块:BeatifulSoup模块
    034 如何判断一个对象是否是可调用对象
    037 简单计算器实现
    036 re模块的小练习
    035 用Python实现的二分查找算法(基于递归函数)
  • 原文地址:https://www.cnblogs.com/dongshenjun/p/14521720.html
Copyright © 2011-2022 走看看