Is there any difference between the Ok() method new ObjectResult()?

35,502

Solution 1

Technically there is no difference between the two approaches.

If you want to look at the code of OkObjectResult then you will see that the OkObjectResult is an ObjectResult that sets the 200 status code, which is the default of ObjectResult already.

The only difference for me is readability in code and your own or your team preferences. It is all about naming and what intention you want to stress.

 return Ok(myResult);                  // gives emphasis on status, my personal favorite

 return new OkObjectResult(myResult);  // for me a little bit verbose and the same
                                       // effect as Ok; but states we return an Object

 return new ObjectResult(myResult);    // gives emphasis of the content that is returned

Solution 2

Update: Both approaches in the original question + the third approach in the accepted answer has now been superseded by simply returning the object directly:

 return myResult

Relevant example and explanation from the current tutorial page:

[HttpGet("{id}")]
public async Task<ActionResult<TodoItem>> GetTodoItem(long id)
{
    var todoItem = await _context.TodoItems.FindAsync(id);    
    if (todoItem == null)
    {
        return NotFound();
    }    
    return todoItem;
}

The return type of the GetTodoItems and GetTodoItem methods is ActionResult< T > type. ASP.NET Core automatically serializes the object to JSON and writes the JSON into the body of the response message. The response code for this return type is 200, assuming there are no unhandled exceptions. Unhandled exceptions are translated into 5xx errors.

Solution 3

I can only see the difference in relying on some default value somewhere and providing this value explicitly - the latter is usually better and your intention is much more clear with OkObjectResult (or setting StatusCode explicitly), which is important enough. ObjectResult does not have default StatusCode of 200 - it actually have this value null by default. However, HttpResponse has default status code 200, so it works the same anyway.

Share:
35,502
PMBjornerud
Author by

PMBjornerud

Updated on April 21, 2021

Comments

  • PMBjornerud
    PMBjornerud about 3 years

    Scenario: implementing a standard REST API / GET method on a .net core controller.

    The documentation states that OkObjectResult is an ObjectResult with status 200. This is available through the Ok(myResult) method inherited from ControllerBase. I assume this is a convenience method.

    However, the tutorial is not using this approach - it instead returns new ObjectResult(myResult) which will default to status 200.

    Is there any difference between these two approaches?

  • Sergey Zykov
    Sergey Zykov over 5 years
    well, actually it is very misleading answer. Based on this doc: docs.microsoft.com/en-us/dotnet/api/… ApiController has two overloads. Ok return OkResult and Ok<T>(T) returns OkObjectResult. The difference is that OkResult is resulting with status code 200 while OkObjectResult tells that it also returns some value.
  • Sergey Zykov
    Sergey Zykov over 5 years
    So its not just 'readability in code'. ApiController.Ok method should be always used.
  • Joerg
    Joerg over 3 years
    How does this work? todoItem is not of type ActionResult<TodoItem>, but of type TodoItem. Why is the compiler not complaining?
  • PMBjornerud
    PMBjornerud over 3 years
    If you need more information than what Microsoft provides in the link above, I suggest you ask your own SO question about this specific part. I don't know the details without looking into it, and a proper answer will probably not fit in a comment anyway.