c# - 来自另一个类库的基本 Controller 在 web api 中不起作用

标签 c# asp.net-web-api controller

我有两个 Web API 项目和一个 MarketController,我需要扩展 Api Controller ,所以我做到了。

我创建了一个 BaseController 类并继承自 ApiController,如下所示:

public class BaseController:ApiController { }

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

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

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

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

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

响应是:

{
    "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'."
}

最佳答案

无需实现自定义 ControllerFactory 或 AssemblyResolver 类。 如果您将 Microsoft.AspNet.WebApi.Core nuget 包添加到包含基类的程序集中,此方案将“正常工作”。

在我的例子中,我只是添加了对将编译的 System.Web.Http.dll 的引用,但 Controller 将无法正确加载。添加 Nuget 包后一切正常,无需更改代码。

关于c# - 来自另一个类库的基本 Controller 在 web api 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25975446/

相关文章:

c# - '<Module>' 的类型初始值设定项抛出异常并且算术运算导致溢出

asp.net-web-api - MVC Web API 不适用于 Autofac 集成

grails - 如何在我的分页 View 中正确显示列表项

css - 编辑 <li> 的 innerhtml 以添加带有图标图像的 <i> 标签

javascript - jquery fancybox + angularJS : fancybox button action issue

javascript - .NET Core中如何使用ajax返回ViewComponent列表进行分页?

c# - 提供存储在服务器上而不是应用程序文件中的图像的路径

c# - 使用 JSON.NET 解析 C# 中的 Json

asp.net-web-api - 在 Service Fabric 中公开 Web API

c# - Asp.Net WebApi OWIN 身份验证