asp.net-mvc 未找到类型或命名空间 IHttpActionResult

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19853196/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 03:18:35  来源:igfitidea点击:

The type or namespace IHttpActionResult not found

asp.net-mvcnamespaces

提问by BPX

This is the first time I am creating a Web API application in ASP.NET MVC4. I opened a new Web API project and added a new controller named 'Product'. The function given below shows an error namespace not found.

这是我第一次在 ASP.NET MVC4 中创建 Web API 应用程序。我打开了一个新的 Web API 项目并添加了一个名为“产品”的新控制器。下面给出的函数显示了未找到的错误命名空间。

public IHttpActionResult GetProduct(int id) 
{
    var product = products.FirstOrDefault((p) => p.Id == id);
    if (product == null)
    {
        return NotFound();
    }
    return Ok(product);
 }

Can anyone help me to solve this problem?

谁能帮我解决这个问题?

回答by Chandra Malla

Since you are using Asp.Net 4.0, IHttpActionResult is not available in Namespace System.Web.Http version 4.0 so you have to update your namespace to version 5.0.0.0. You can Check your namespace version by right click System.Web.Http namespace in references and go to properties. You can update this by Right Clicking on your Solution project->Manage NuGet Packages for Solution-> Search for Asp.Net WebAPI 2 ->Select Asp.Net WebAPI 2 and click Manage. Also you can check if it is available on update tab, if it is available in update tab you can update from there. Once you did that, confirm version by going to properties of System.Web.Http namespace in references.

由于您使用的是 Asp.Net 4.0,因此 IHttpActionResult 在命名空间 System.Web.Http 4.0 版中不可用,因此您必须将命名空间更新到 5.0.0.0 版。您可以通过右键单击引用中的 System.Web.Http 命名空间并转到属性来检查命名空间版本。您可以通过右键单击解决方案项目-> 管理解决方案的 NuGet 包-> 搜索 Asp.Net WebAPI 2 -> 选择 Asp.Net WebAPI 2 并单击管理来更新它。您也可以检查它是否在更新选项卡上可用,如果它在更新选项卡中可用,您可以从那里更新。完成此操作后,通过转到引用中 System.Web.Http 命名空间的属性来确认版本。

This works for me and hope will work for you too.

这对我有用,希望对你也有用。

Thanks

谢谢

回答by BPX

I had a similar problem when I checked out a project from my source control.

当我从源代码管理中检出一个项目时,我遇到了类似的问题。

I had the reference to Microsoft ASP.NET Web API 2 in the project but i still couldn't compile the project.

我在项目中引用了 Microsoft ASP.NET Web API 2,但我仍然无法编译该项目。

The solution was to re install the package using the Package Manager Console:

解决方案是使用包管理器控制台重新安装包:

PM> update-Package Microsoft.AspNet.WebApi –reinstall

PM> update-Package Microsoft.AspNet.WebApi –reinstall

回答by Catto

To resolve the IHttpActionResult reference error add a reference using NuGet to install the Web API2 Core lib. dll

要解决 IHttpActionResult 引用错误,请使用 NuGet 添加引用以安装 Web API2 核心库。dll

In solution explorer in the references right click and select manage nuget packages. (if not there install nuget)

在解决方案资源管理器中,右键单击并选择管理 nuget 包。(如果没有安装nuget)

In the manage NuGet Packages window on the left side click online then in top right search for API 2.1 and install Microsoft ASP.NET Web API 2.1 Core Libraries. (Once installed the install button will change to a green check)

在左侧的管理 NuGet 包窗口中单击在线,然后在右上角搜索 API 2.1 并安装 Microsoft ASP.NET Web API 2.1 核心库。(安装后,安装按钮将变为绿色对勾)

enter image description here

enter image description here

After that the project will reload and when it's build again the IHttpActionResult reference error will be resolved and the project will build successful. Your question was good and helped me. Hope this answer helps!

之后项目将重新加载,当它再次构建时,IHttpActionResult 引用错误将得到解决,项目将成功构建。你的问题很好,对我有帮助。希望这个回答有帮助!

回答by kasim

Check with this link to get the namespace for IHttpActionResultand all.

检查此链接以获取 IHttpActionResult和所有的命名空间

This package contains everything you need to host ASP.NET Web API on IIS. ASP.NET Web API is a framework that makes it easy to build HTTP services that reach a broad range of clients, including browsers and mobile devices. ASP.NET Web API is an ideal platform for building RESTful applications on the .NET Framework.

此包包含在 IIS 上托管 ASP.NET Web API 所需的一切。ASP.NET Web API 是一个框架,可让您轻松构建可访问各种客户端(包括浏览器和移动设备)的 HTTP 服务。ASP.NET Web API 是在 .NET Framework 上构建 RESTful 应用程序的理想平台。

To install Microsoft ASP.NET Web API 2, run the following command in the Package Manager Console Or Manage NuGet Packages

要安装 Microsoft ASP.NET Web API 2,请在包管理器控制台或管理 NuGet 包中运行以下命令

PM> Install-Package Microsoft.AspNet.WebApi -Version 5.0.0

PM> 安装包 Microsoft.AspNet.WebApi -Version 5.0.0

回答by Kuntal Ghosh

I had similar problem & re installation will solve this issue. Just go to Tools > NuGet Package Manager > Package Manager Console in Visual Studio 2013 and write down the following :-

我有类似的问题,重新安装将解决这个问题。只需在 Visual Studio 2013 中转到“工具”>“NuGet 包管理器”>“包管理器控制台”并记下以下内容:-

PM> update-Package Microsoft.AspNet.WebApi –reinstall

Then, Clean the solution and Build/Rebuild the solution.

然后,清理解决方案并构建/重建解决方案。

Note: In my case, the process explicitly checked out Web.config and packages.config file and also removed the System.Web.Http dll file.

注意:就我而言,该过程明确检出 Web.config 和 packages.config 文件,还删除了 System.Web.Http dll 文件。

回答by z0mbi3

This solution worked for me:
Tools – > Library Package Manager -> Package Manager Console, then type:
Install-Package Microsoft.AspNet.WebApi -Version 5.0.0
update-Package Microsoft.AspNet.WebApi –reinstall
Reference: http://blog.bitlinkit.com/type-namespace-ihttpactionresult-not-found/

这个解决方案对我 有用:
工具 -> 库包管理器 -> 包管理器控制台,然后输入:
Install-Package Microsoft.AspNet.WebApi -Version 5.0.0
update-Package Microsoft.AspNet.WebApi -reinstall
参考:http:// blog.bitlinkit.com/type-namespace-ihttpactionresult-not-found/

回答by Matthew K

The features you are trying to use are part of Web API 2. Since you mentioned you're using MVC 4, I expect you will want to upgrade your references to MVC 5 and Web API 2.

您尝试使用的功能是 Web API 2 的一部分。既然您提到您使用的是 MVC 4,我希望您希望将您的参考升级到 MVC 5 和 Web API 2。

Find out more about these features at http://www.strathweb.com/2013/06/ihttpactionresult-new-way-of-creating-responses-in-asp-net-web-api-2/

http://www.strathweb.com/2013/06/ihttpactionresult-new-way-of-creating-responses-in-asp-net-web-api-2/了解有关这些功能的更多信息

回答by mouse m.d.

In my case after updating System.Web.Httpto version 5.0.0.0, I needed to restart the IDE in order to fix this problem.

就我而言,更新System.Web.Http到 5.0.0.0 版后,我需要重新启动 IDE 才能解决此问题。

回答by anhdktk

Using NuGet to install the Web API2 Core lib. dll. You may have to update Nuget before install ing Web API2 because some components in API2 not compatible with old Nuget package. I had done this way and problem was resolved.

使用 NuGet 安装 Web API2 核心库。dll。您可能需要在安装 Web API2 之前更新 Nuget,因为 API2 中的某些组件与旧的 Nuget 包不兼容。我已经这样做了,问题解决了。

回答by kas

This issue seems to happen in 2 recent projects I have started using visual studio 2015. Project is an ASP.Net MVC 5 website with mvc and web api components. After running the updates and cleaning up unwanted nuget packages I copied some boilerplate code from another similar project and had the same issue with IHttpActionResult and RouteAttribute not found.

这个问题似乎发生在我最近开始使用 Visual Studio 2015 的 2 个项目中。项目是一个带有 mvc 和 web api 组件的 ASP.Net MVC 5 网站。在运行更新并清理不需要的 nuget 包后,我从另一个类似项目中复制了一些样板代码,并且遇到了与 IHttpActionResult 和 RouteAttribute 未找到的相同问题。

The problem was due to the incorrect Version of the System.Web.Http.dll referenced in the project references. By default the system GAC version of the assembly is used.

问题是由于项目引用中引用的 System.Web.Http.dll 版本不正确。默认情况下,使用程序集的系统 GAC 版本。

To fix this, simply remove this reference and add a reference to the same assembly in the Nuget package folder: [project folder]\packages\Microsoft.AspNet.WebApi.Core.5.2.3\lib\net45\System.Web.Http.dll

要解决此问题,只需删除此引用并在 Nuget 包文件夹中添加对同一程序集的引用:[项目文件夹]\packages\Microsoft.AspNet.WebApi.Core.5.2.3\lib\net45\System.Web.Http .dll

You should check that your have updated to the required version of Microsoft.AspNet.WebApi.Core nuget package first.

您应该首先检查您是否已更新到所需版本的 Microsoft.AspNet.WebApi.Core nuget 包。