ApiController和IHttpActionResult

时间:2016-05-09 13:16:24

标签: c# asp.net

我创建了一个基于ASP.NET Web API的应用程序,并遵循以下教程: http://www.asp.net/web-api/overview/getting-started-with-aspnet-web-api/action-results
enter image description here

我正在尝试将固有的ApiController类作为教程中的描述,并且还要实现IHttpActionResult接口,但编译器抱怨说,找不到它们的程序集。

我的问题是,如何为ApiControllerIHttpActionResult添加程序集?

更新1

我通过nuget添加Microsoft.AspNet.WebApi.Core并显示错误消息 enter image description here

更新2 错误信息
enter image description here
enter image description here

2 个答案:

答案 0 :(得分:3)

您正在使用ASP.NET Web API 2的教程,但您选择的项目模板是针对ASP.NET Core的。这是两种不同的野兽。要学习本教程,您应该从“ASP.NET 4.6.1”区域选择“Web API”模板,或use a tutorial for ASP.NET Core

ASP.NET Core(previous called ASP.NET 5)专为多平台开发而设计,可与MVC6一起运行。 official documentation中有一个很好的概念性概述。以下是与此问题相关的一小部分内容:

  

使用ASP.NET 5,我们正在进行许多架构更改   使核心Web框架更精简,更模块化。 ASP.NET 5是   不再基于System.Web.dll,而是基于一组   粒度和良好的NuGet包,允许您进行优化   您的应用程序可以满足您的需求。

在Core中,所有控制器(包括API控制器)都继承自$workplace = $this->Vacancies->Cities->find(...)->where(...); 基类。 DNXCORE50不支持您尝试添加的程序集,因为它们不是跨平台的。

答案 1 :(得分:0)

将此添加到您的使用中:

using System.Web.Http;

另外,添加这个nuget-package:

https://www.nuget.org/packages/Microsoft.AspNet.WebApi.Core/

在您的包管理器中运行:

Install-Package Microsoft.AspNet.WebApi.Core

如果您想要特定版本,可能还是这样:

Install-Package Microsoft.AspNet.WebApi.Core -version 5.2.3

它经常将此添加到您的web.config:

  <dependentAssembly>
    <assemblyIdentity name="System.Web.Http" publicKeyToken="31bf3856ad364e35" culture="neutral" />
    <bindingRedirect oldVersion="0.0.0.0-5.2.3.0" newVersion="5.2.3.0" />
  </dependentAssembly>