来自另一个类库的基本控制器在 web api 中不起作用 [英] Base controller from another class library doesn't working in web api

查看:31
本文介绍了来自另一个类库的基本控制器在 web api 中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个 Web API 项目,我有一个 MarketController,我需要扩展 Api 控制器,所以我做到了.

I have two Web API projects and I have a MarketController and I need to extend the Api controller so I did it.

我创建了一个 BaseController 类并从 ApiController 继承如下:

I created a BaseController class and inherit from ApiController like this:

public class BaseController:ApiController { }

到目前为止一切顺利,一切正常:

so far so good, it's working fine:

public class MarketController : BaseController
{
    public MarketController() : base()
    {
    }

    // GET api/<controller>
    public IEnumerable<string> Get()
    {
        return new string[] { "value1", "value2" };
    }
}

但我想在另一个名为 BLL 的类库中完成它,所以我将 BaseController 类移到了 BLL 类库中,然后我在 Web API 项目中引用了它.

But I wanted to do it in another class library called BLL, so I moved the BaseController class to the BLL class library and I referenced it in the Web API project.

当我这样做时,api 停止工作.

When I did this, the api stopped working.

回复是:

{
    "Message": "No HTTP resource was found that matches the request URI someurl/api/market.",
    "MessageDetail": "No type was found that matches the controller named 'market'."
}

推荐答案

默认情况下,MVC 会在 mvc 应用程序的同一程序集中查找所有控制器.默认控制器工厂基于字符串ControllerName+Controller"创建控制器实例,例如 MarketController,其中市场来自 url market/actionname.它将在与 mvc 应用程序相同的程序集中寻找 MarketController.

By default MVC looks for all controllers in same assembly of mvc application. The default controller factory creates controller instance based on string 'ControllerName+Controller' like MarketController where market comes from url market/actionname.It will look for MarketController in the same assembly as mvc application.

要将控制器放在单独的程序集中,您必须创建自己的控制器工厂,或者您必须指定程序集名称为 app start.

To put controller in separate assembly you will have to create your own controller factory or you will have to specify assembly name is app start.

创建自己的自定义 ControllerFactory 后,将以下行添加到 global.asax 中的 Application_Start 以告诉框架在哪里可以找到它:

Once you've created your own custom ControllerFactory, you add the following line to Application_Start in global.asax to tell the framework where to find it:

ControllerBuilder.Current.SetControllerFactory(new MyControllerFactory());

或者对于像您这样的简单情况,您可以这样做:

Or for simple cases like yours you can do this :

routes.MapRoute(
    "Default",
    "{controller}/{action}/{id}",
    new { controller = "Home", action = "Index", id = "" },
    new[] { "BLLAssembly.Controllers" }
);

这里的 BLLAssembly.Controllers 是 BLL 程序集中的 BaseController 的命名空间.

Here BLLAssembly.Controllers is namespace for your BaseController in BLL assembly.

使用自定义程序集解析器还有一种更高级的方法,即 IAssembliesResolver下面的文章也讲述了如何使用 Web Api 来做到这一点,

There is one more advanced way using custom assembly resolver ,i.e IAssembliesResolver The below article tells how to do this with Web Api also,

http://www.strathweb.com/2012/06/using-controllers-from-an-external-assembly-in-asp-net-web-api/

这篇关于来自另一个类库的基本控制器在 web api 中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆