The type or namespace IHttpActionResult not found

BPX picture BPX · Nov 8, 2013 · Viewed 80.9k times · Source

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?

Answer

Chandra Malla picture Chandra Malla · Jan 4, 2014

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