zoukankan      html  css  js  c++  java
  • asp.net core 配置

    
    
    ASP.NET Core的配置系统已经和之前版本的ASP.NET有所不同了,之前是依赖于System.Configuration和XML配置文件web.config,现在支持各种格式的配置,比以前灵活多了。
    
    
    支持一下几种:
    
    
    MemoryConfigurationProvider
    
    
    EnvironmentVariablesConfigurationProvider
    
    
    CommandLineConfigurationProvider
    
    
    JsonConfigurationProvider
    
    
    XmlConfiguationProvider
    
    
    IniConfigurationProvider
    
    
    自定义ConfigurationProvider
    
    
     
    
    
    今天我们看看JsonConfig文件的使用:
    
    
    Core中配置涉及的类结构
    
     
    
    
     
    
     
    
    ConfgurationSource:相当于配饰的数据源,可以是内存,可以是配置文件。
    
    ConfigurationBuilder :配饰数据源的组装车间,负责取出数据源供配置类消费Configuration。
    
    Configuration :配置数据的映射
    
     
    
    配置类定义:
    
     
    
    public class CompanyInfoOptions
    
    {
    
    public CompanyInfoOptions(IConfiguration config)
    
    {
    
    this.Name=config["name"];
    
    this.Address=config["address"];
    
    this.CreateDate = DateTime.Parse( config["createDate"]);
    
    }
    
    public String Name
    
    {
    
    get;
    
    set;
    
    }
    
     
    
    public String Address
    
    {
    
    get;
    
    set;
    
    }
    
     
    
    public DateTime CreateDate
    
    {
    
    get;
    
    set;
    
    }
    
    }
    
     
    VSCode 文件组织形式:
    
    
    {
    "companyInfo": {
    "Name": "hbb0b0",
    "address": "Xi'AN JinYeLu number6",
    "createDate": "2017/08/06"
    }
    }

    注意配置文件在运行目录,与以前配置不同的配置文件不用拷贝到Bin目录。

     
    
    Main中调用:
    
     
    
    class Program
    
    {
    
    //Section 节名称
    
    const string str_SectionCompanyInfo="companyInfo";
    
    static void Main(string[] args)
    
    {
    
    Console.WriteLine(Directory.GetCurrentDirectory());
    
    //ConfigtionBuilder初始化
    
    var config = new ConfigurationBuilder()
    
    .SetBasePath(Directory.GetCurrentDirectory())
    
    .AddJsonFile("companyinfo.json", optional:true,reloadOnChange:true)
    
    .Build();
    
    //初始化CompanyInfoOptions
    
    CompanyInfoOptions options= new CompanyInfoOptions(config.GetSection(str_SectionCompanyInfo));
    
     
    
    //输出
    
    Console.WriteLine($"name:{options.Name}");
    
    Console.WriteLine($"address:{options.Address}");
    
    Console.WriteLine($"createDate:{options.CreateDate.ToString()}");
    
    }
    
    }

  • 相关阅读:
    Mac上的USB存储设备使用痕迹在新版操作系统有所变化
    Beware of the encrypted VM
    A barrier for Mobile Forensics
    Second Space could let suspect play two different roles easily
    Take advantage of Checkra1n to Jailbreak iDevice for App analysis
    Find out "Who" and "Where"
    Where is the clone one and how to extract it?
    Downgrade extraction on phones running Android 7/8/9
    高版本安卓手机的取证未来
    How to extract WeChat chat messages from a smartphone running Android 7.x or above
  • 原文地址:https://www.cnblogs.com/hbb0b0/p/7293966.html
Copyright © 2011-2022 走看看