本文介绍了可在Ninject绑定将基于URL /路由价值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我要在其上实现相同的接口两个不同的实体使用CRUD操作单个控制器。我想对Ninject给它基于URL查询字符串值(或者一个不同的URL,路由到同一个控制器)不同的存储库。这可能吗?我该怎么办呢?

I have a single controller that I want to use for CRUD operations on two different entities which implement the same interface. I'd like for Ninject to give it a different repository based on a query string value in the URL (or maybe a different URL, routed to the same controller). Is this possible? How can I do it?

推荐答案

这通常是一个设计的味道,但你可以这样定义绑定:

That's usually a design smell but you could define the binding like this:

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

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

这篇关于可在Ninject绑定将基于URL /路由价值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-30 06:20