- 命令行的配置
var settings = new Dictionary<string, string>{ { "name","cyao"}, {"age","18"} }; var builder = new ConfigurationBuilder() .AddInMemoryCollection(settings)内存中获取appsettings .AddCommandLine(args); var configuration = builder.Build(); Console.WriteLine($"name:{configuration["name"]}"); Console.WriteLine($"age:{configuration["age"]}"); Console.ReadLine(); Console.ReadKey();
- json文件的配置
首先新建一个class.json的配置文件 { "classNo": "1", "gread": "1", "student": [ { "name": "cyao", "age": 18 }, { "name": "jaya", "age": 19 } ] }
然后在configuration中注册
var builder = new ConfigurationBuilder() .AddJsonFile("class.json")//json文件中获取appsettings //.AddInMemoryCollection(settings)内存中获取appsettings .AddCommandLine(args); var configuration = builder.Build(); Console.WriteLine($"classNo:{configuration["classNo"]}"); Console.WriteLine($"gread:{configuration["gread"]}"); Console.WriteLine($"name:{configuration["student:0:name"]}"); Console.WriteLine($"age:{configuration["student:0:age"]}"); Console.WriteLine($"name:{configuration["student:1:name"]}"); Console.WriteLine($"age:{configuration["student:1:age"]}"); Console.ReadLine(); Console.ReadKey();
- 从配置文件文本到C#对象实例的映射-option与Bind
namespace JwtAuth { public class JwtSettings { ///使用者 public string Issuer { get; set; } ///颁发者 public string Audience { get; set; } ///秘钥必须大于16个字符 public string SecretKey { get; set; } } }
json文件格式如下
{ "Logging": { "IncludeScopes": false, "Debug": { "LogLevel": { "Default": "Warning" } }, "Console": { "LogLevel": { "Default": "Warning" } } }, "JwtSettings": { "Issuer": "http://locahost:5000", "Audience": "http://locahost:5000", "SecretKey": "hello world this is my key for cyao" } }using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; using Microsoft.Extensions.Options; namespace JwtAuth { using Microsoft.AspNetCore.Authentication.JwtBearer; using Microsoft.AspNetCore.Authorization; using Microsoft.IdentityModel.Tokens; public class Startup { public Startup(IConfiguration configuration) { Configuration = configuration; } public IConfiguration Configuration { get; } // This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { //将配置文件读取到settings services.Configure<JwtSettings>(Configuration.GetSection("JwtSettings")); JwtSettings settings = new JwtSettings(); Configuration.Bind("JwtSettings", settings); services.AddMvc(); } // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app, IHostingEnvironment env) { if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } app.UseMvc(); } } }
- 配置文件热更新
<ItemGroup> <DotNetCliToolReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Tools" Version="2.0.3"/> <DotNetCliToolReference Include="Microsoft.DotNet.Watcher.Tools" Version="2.0.0"/> </ItemGroup>
- 框架设计:Configuration