ok() vs okResult() vs okObjectResult()

Newbie Dev 156 Reputation points
2021-12-06T20:31:24.32+00:00

Hi,

I am looking at the Microsoft documentation for the Ok(),OkResult and okObjectResult.

https://learn.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.mvc.okobjectresult?view=aspnetcore-6.0

https://learn.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.mvc.okresult?view=aspnetcore-6.0

Can someone please give me an example of how they are different?
What will be the output of each of them?

Thanks in advance

Developer technologies | ASP.NET | ASP.NET Core
0 comments No comments
{count} votes

Accepted answer
  1. Anonymous
    2021-12-07T02:05:06.927+00:00

    Hi @Newbie Dev ,

    Can someone please give me an example of how they are different?
    What will be the output of each of them?

    If you check the Ok constructor method, you can see that:

    155424-image.png

    When using the Ok method without parameter, it will return an OkResult object. If using the Ok method with the parameter (an object), it will return an OkObjectResult object.

    The difference between OkResult and OkObjectResult as the document and Bruce-SqlWork said, the OkResult will show an empty 200 response, and the OkObjectResult will show the 200 responses with the passed object.

    So, the code return Ok(); equals return new OkResult(); , and the code return Ok(object); equals return new OkObjectResult(object);. The only difference for me is readability of code and your own or your team preferences.

    The result is like this:

    using OkObjectResult or Ok(object):

        public IActionResult Index2()  
        {  
            return new OkObjectResult(new { userid=101, username="Tom"});   // or using  return Ok(new { userid=101, username="Tom"});   
        }  
    

    155397-capture1.png

    Using OkResult or Ok():

        public IActionResult Index2()  
        {  
            return new OkResult();     // Or  using  return Ok();   
        }  
    

    155463-capture.png


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    Best regards,
    Dillion

    6 people found this answer helpful.

1 additional answer

Sort by: Most helpful
  1. Bruce (SqlWork.com) 78,086 Reputation points Volunteer Moderator
    2021-12-06T22:06:24.44+00:00

    Ok() is a controller method that returns a new OkResult();
    Ok(object) is a controller method that returns a new OkObjectResult(object);

    the OkResult class implements an ActionResult that produces an empty 200 response
    the OkObjectResult class implements an ActionResult that process a 200 response with a body based on the passed object.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.