0

I have installed AspNetWebApi 5.2.3 using Nuget Package Manager in my ASP.Net MVC project however System.Web.Http.Ok does not resolve. My project is configured for .Net Framework 4.6.1. I'm going to use below syntax in my code, however Ok does not resolve:

return Ok(new { Status = 1, CustomerId = 1});

What I have missed here?

VSB
  • 9,825
  • 16
  • 72
  • 145

1 Answers1

2

The Ok method is available on ApiController in the System.Web.Http namespace which you correctly reference. When adding Web API to your application you need to make sure that your API controllers inherit from the ApiController class instead of Controller for MVC.

https://msdn.microsoft.com/en-us/library/system.web.http.apicontroller(v=vs.118).aspx

benjrb
  • 766
  • 1
  • 5
  • 13
  • And one another point, when I Inherited from ApiController, I should change return value of my methods to `IHttpActionResult` and my controller will not accessible any more! I mean when I call the same route before this change everything was fine but now calling my controller I face ` The resource cannot be found.` error prompt – VSB Feb 03 '18 at 13:51
  • 1
    Try `api/` before your route. It might be looking for your controller using MVC – benjrb Feb 03 '18 at 13:58
  • It also requires register routes for web api to `Application_Start()` method as mentioned in [this answer](https://stackoverflow.com/a/21936073/1080355) – VSB Feb 03 '18 at 14:07