配置文件的读取和使用
cs
//读取配置文件直接使用
var configModel = configuration.GetSection("DataBaseConfig").Get<DataBaseConfigModel>();
//读取配置文件注入到IOC中
services.Configure<AssemblyConfig>(configuration.GetSection("AssemblyConfig"));
.net core3.1
cs
public class Startup
{
/// <summary>
/// 配置文件读取类
/// </summary>
public Microsoft.Extensions.Configuration.IConfiguration Configuration { get; }
/// <summary>
/// 构造函数注入
/// </summary>
/// <param name="configuration"></param>
public Startup(Microsoft.Extensions.Configuration.IConfiguration configuration)
{
Configuration = configuration;
}
/// <summary>
/// 服务注册
/// </summary>
/// <param name="services"></param>
public void ConfigureServices(IServiceCollection services)
{
//services
}
/// <summary>
/// 服务配置
/// </summary>
/// <param name="app"></param>
/// <param name="env"></param>
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
//App和Env
}
}
.net 6
cs
var builder = WebApplication.CreateBuilder(args);
ConfigurationManager configuration = builder.Configuration;
var env = builder.Environment;
var imServerSetting = configuration.GetSection("Option").Get<ServerConfigOptions>();
var app = builder.Build();
//app.Map();
app.Run();