asp.net core 6.0 访问IConfiguration


using System.ComponentModel.DataAnnotations;
using Microsoft.EntityFrameworkCore;

var builder = WebApplication.CreateBuilder(args);

var connectionString = builder.Configuration.GetConnectionString("TodoDb") 
                       ?? "Data Source=todos.db";


builder.Services.AddSqlite<TodoDb>(connectionString)
                .AddDatabaseDeveloperPageExceptionFilter();

WebApplicationBuilder.Configuration属性是一个实现IConfigurationRootIConfiguration的Microsoft.Extensions.ConfigurationManager实例,因此可以使用它加载配置设置或使用类似GetConnectionString的扩展方法

构建应用程序后,可以通过WebApplication.Configuration属性访问配置。这只是对Services.GetRequiredService<IConfiguration>()的一个调用:

public IConfiguration Configuration => 
    _host.Services.GetRequiredService<IConfiguration>();

来自:https://www.5axxw.com/questions/content/drfzx5


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM