如何根据路由值选择配置部分

发布于 2025-02-13 21:19:47 字数 1270 浏览 2 评论 0原文

我有一个ASP.NET Core 6 MVC网站。它还使用实体框架核心。我想配置我的网站,以便路由的第一部分定义要使用的配置部分。

我的appSettings.json文件将具有多个部分,所有部分都具有相同的结构,只有某些设置值才更改。它看起来像这样:

"standardConfigs": {
    "ConnectionString": "Server=MyStandardServer;Database=MyStandardDB;user id=*****;password=*****;MultipleActiveResultSets=true",  
    "AdditionalSettings": "SomeValue",  
},
"upgradedConfigs": {
    "ConnectionString": "Server=MyUpgradedServer;Database=MyUpgradedDB;user id=*****;password=*****;MultipleActiveResultSets=true",  
    "AdditionalSettings": "SomeOtherValue",  
},

startup.cs中,Entity Framework Core配置为使用配置中的连接字符串。如果用户访问url http://example.com/stanpleard/somecontroller/someaction/,我想获得路由的第一部分(在这种情况下为'标准')并使用它来使用它来从连接字符串到达​​的位置选择“配置”部分('StandardConfigs')。

它应该看起来像这样:

var configSectionName = GetFirstPartOfRoute(); // How to do this?
var dbConfig = Configuration
        .GetSection(configSectionName + "Configs")
        .Get<MyConfiguration>();
services.AddDbContext<URMSContext>(options =>
                options.UseLazyLoadingProxies().UseSqlServer(dbConfig.ConnectionString)
                    );

如何在我的网站上配置它?这是我如何使其工作的总体想法。如果您有一个更好的主意,可以根据路由值选择配置,请随时共享。

I have an ASP.NET Core 6 MVC website. It also uses Entity Framework Core. I want to configure my website so that the first part of the route defines which configuration section to use.

My appsettings.json file will have multiple sections, all with same structure, just some settings value is changed. It will look something like this:

"standardConfigs": {
    "ConnectionString": "Server=MyStandardServer;Database=MyStandardDB;user id=*****;password=*****;MultipleActiveResultSets=true",  
    "AdditionalSettings": "SomeValue",  
},
"upgradedConfigs": {
    "ConnectionString": "Server=MyUpgradedServer;Database=MyUpgradedDB;user id=*****;password=*****;MultipleActiveResultSets=true",  
    "AdditionalSettings": "SomeOtherValue",  
},

In StartUp.cs, Entity Framework Core is configured to use a connection string from configuration. If the user hits the url http://example.com/standard/somecontroller/someaction/, I want to get the first part of the route ('standard' in this case) and use it to select the configuration section ('standardConfigs') from where the connection string will come.

It should look like this:

var configSectionName = GetFirstPartOfRoute(); // How to do this?
var dbConfig = Configuration
        .GetSection(configSectionName + "Configs")
        .Get<MyConfiguration>();
services.AddDbContext<URMSContext>(options =>
                options.UseLazyLoadingProxies().UseSqlServer(dbConfig.ConnectionString)
                    );

How to configure this in my website? This is my general idea how to make it work. If you have a better idea to select configuration based on route value feel free to share.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

原野 2025-02-20 21:19:47

遵循文档,您可以亚类dbcontext和覆盖方法覆盖。

您必须注入 ihttpContextAccessor 访问DBContext中的路线。

确保按照范围添加您的新dbcotnext。

Following the documentation, you can subclass DbContext and override OnConfiguring method.

You have to inject IHttpContextAccessor as well in order to access the route in the DbContext.

Make sure to add Your new DbCotnext as scoped.

漫雪独思 2025-02-20 21:19:47

在program.cs中执行此操作

builder.Services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
builder.Services.AddDbContext<URMSContext>((serviceProvider, dbContextBuilder) =>
{
    var httpContextAccessor = serviceProvider.GetRequiredService<IHttpContextAccessor>();
    
    // extract configsection from this path
    var configSectionName = httpContextAccessor.HttpContext.Request.Path; 
    
    var dbConfig = Configuration
        .GetSection(configSectionName + "Configs")
        .Get<MyConfiguration>();
    dbContextBuilder.UseSqlServer(dbConfig.ConnectionString);
});

do this in program.cs

builder.Services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
builder.Services.AddDbContext<URMSContext>((serviceProvider, dbContextBuilder) =>
{
    var httpContextAccessor = serviceProvider.GetRequiredService<IHttpContextAccessor>();
    
    // extract configsection from this path
    var configSectionName = httpContextAccessor.HttpContext.Request.Path; 
    
    var dbConfig = Configuration
        .GetSection(configSectionName + "Configs")
        .Get<MyConfiguration>();
    dbContextBuilder.UseSqlServer(dbConfig.ConnectionString);
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文