asp.net core获取config和env

配置文件的读取和使用

//读取配置文件直接使用
var configModel = configuration.GetSection("DataBaseConfig").Get<DataBaseConfigModel>();
//读取配置文件注入到IOC中
services.Configure<AssemblyConfig>(configuration.GetSection("AssemblyConfig"));

.net core3.1

public class Startup
{
    /// 
    /// 配置文件读取类
    /// 
    public Microsoft.Extensions.Configuration.IConfiguration Configuration { get; }

    /// 
    /// 构造函数注入
    /// 
    /// 
    public Startup(Microsoft.Extensions.Configuration.IConfiguration configuration)
    {
        Configuration = configuration;
    }

    /// 
    /// 服务注册
    /// 
    /// 
    public void ConfigureServices(IServiceCollection services)
    {
        //services
    }

    /// 
    /// 服务配置
    /// 
    /// 
    /// 
    public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
    {
        //App和Env
    }
}

.net 6

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();

你可能感兴趣的:(dotnet,asp.net,java,前端)