如何根据路由值选择配置部分
我有一个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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
遵循文档,您可以亚类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.
在program.cs中执行此操作
do this in program.cs