The type or namespace IHttpActionResult not found

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.

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?


Solution 1:

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.

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

Thanks

Solution 2:

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.

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

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