IHttpActionResult and helper methods in ASP.NET Core

Tim picture Tim · May 28, 2015 · Viewed 24.2k times · Source

I'm trying to move my web api 2 project to ASP.NET 5. But I have many elements that are not present anymore.

For example IHttpActionResult or Ok(), NotFound() methods. Or RoutePrefix[]

Should I change every IHttpActionResult with IActionResult ? Change Ok() with new ObjectResult ? (is it the same ?)

What about HttpConfiguration that seems no more present in startup.cs ?

Answer

Sean picture Sean · Aug 12, 2015

IHttpActionResult is now effectively IActionResult, and to return an Ok with a return object, you'd use return new ObjectResult(...);

So effectively something like this:

public IActionResult Get(int id)
{
    if (id == 1) return HttpNotFound("not found!");
    return new ObjectResult("value: " + id);
}

Here's a good article with more detail:

http://www.asp.net/vnext/overview/aspnet-vnext/create-a-web-api-with-mvc-6