gpt4 book ai didi

asp.net-mvc - Ninject 绑定(bind)可以基于 URL/路由值吗?

转载 作者:行者123 更新时间:2023-12-04 23:59:14 26 4
gpt4 key购买 nike

我有一个 Controller ,我想将其用于实现相同接口(interface)的两个不同实体上的 CRUD 操作。我希望 Ninject 根据 URL 中的查询字符串值(或者可能是不同的 URL,路由到同一个 Controller )给它一个不同的存储库。这可能吗?我该怎么做?

最佳答案

这通常是一种设计气味,但您可以像这样定义绑定(bind):

kernel.Bind<IRepo>().ToMethod(ctx => 
{
var a = HttpContext.Current.Request["a"];
if (a == "b")
{
return new RepoA();
}

return new RepoB();
}).InRequestScope();

关于asp.net-mvc - Ninject 绑定(bind)可以基于 URL/路由值吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12675977/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com