Action Result in ASP.NET Core API

Introduction 

 
This article overview action result which are used in ASP.NET Core and Core API. We will understand both, which are available in two different assemblies of ASP.NET Core Microsoft.AspNetCore.Mvc and System.Web.Http.
 

ObjectResult

 
ObjectResult primary role is content negotiation. It has some variation of a method called SelectFormatter on its ObjectResultExecutor. You can return an object with it, and it formats the response based on what the user requested in the Accept header. If the header didn’t exist, it returns the default format configured for the app. It’s important to note that if the request is issued through a browser, the Accept header will be ignored, unless we set the RespectBrowserAcceptHeader to true when we configure the MVC options in Startup.cs. Also, it doesn’t set the status code, which causes the status code to be null. ObjectResult is the super type of following:
  1. AcceptedResult
  2. AcceptedAtActionResult
  3. AcceptedAtRouteResult
  4. BadRequestObjectResult
  5. CreatedResult
  6. CreatedAtActionResult
  7. CreatedAtRouteResult
  8. NotFoundObjectResult
  9. OkObjectResult
AcceptedResult
 
An AcceptedResultthat returns an Accepted (202) response with a Location header. It indicates that the request is successfully accepted for processing, but it might or might not acted upon. In this case, we should redirect the user to a location that provides some kind of monitor on the current state of the process. For this purpose, we pass a URI.
  1. public AcceptedResult AcceptedActionResult()  
  2.         {  
  3.             return Accepted(new Uri("/Home/Index", UriKind.Relative), new { FirstName = "Farhan",LastName="Ahmed" });  
  4.         }  
AcceptedAtActionResult
 
An AcceptedAtActionResult action result returns an accepted 202 response with a location header.
  1. public AcceptedAtActionResult AcceptedAtActionActionResult()  
  2. {  
  3.    return AcceptedAtAction("IndexWithId""Home"new { Id = 2, area = "" }, new { FirstName = "Farhan",LastName="Ahmed" });  
  4. }  
AcceptedAtRouteResult
 
An AcceptedAtRouteResult returns an Accepted (202) response with a Location header. It's the same as AcceptedResult, with the only difference being that it takes a route name and route value instead of URI.
  1. public AcceptedAtRouteResult AcceptedAtRouteActionResult()  
  2. {  
  3.    return AcceptedAtRoute("default"new { Id = 2, area = "" }, new { FirstName = "Farhan", LastName = "Ahmed" });  
  4. }  
BadRequestResult
 
An ObjectResult, when executed. will produce a Bad Request (400) response. It indicates a bad request by user. It does not take any argument.
  1. public BadRequestResult BadRequestActionResult()  
  2. {  
  3.       return BadRequest();  
  4. }  
BadRequestObjectResult
 
This is similar to BadRequestResult, with the difference that it can pass an object or a ModelStateDictionary containing the details regarding the error.
  1. public BadRequestObjectResult BadRequestObjectActionResult()  
  2. {  
  3.             var modelState = new ModelStateDictionary();  
  4.             modelState.AddModelError("Name""Name is required.");  
  5.             return BadRequest(modelState);  
  6. }  
CreatedResult
 
CreatedResult returns a Created (201) response with a Location header. This indicates the request has been fulfilled and has resulted in one or more new resources being created.
  1. public CreatedResult CreatedActionResult()  
  2.         {  
  3.             return Created(new Uri("/Home/Index", UriKind.Relative), new { FirstName = "Farhan", LastName = "Ahmed" });  
  4.         }  
CreatedAtActionResult
 
CreatedAtActionResult that returns a Created (201) response with a Location header.
  1. public CreatedAtActionResult CreatedAtActionActionResult()  
  2.         {  
  3.             return CreatedAtAction("IndexWithId""Home"new { id = 2, area = "" }, new { FirstName = "Farhan", LastName = "Ahmed" });  
  4.         }  
CreatedAtRouteResult
 
CreatedAtRouteResult that returns a Created (201) response with a Location header.
  1. public CreatedAtRouteResult CreatedAtRouteActionResult()  
  2.         {  
  3.             return CreatedAtRoute("default"new { Id = 2, area = "" }, new { FirstName = "Farhan", LastName = "Ahmed" });  
  4.         }  
NotFoundResult
 
This represents a StatusCodeResult that when executed, will produce a Not Found (404) response.
  1. public NotFoundResult NotFoundActionResult()  
  2.         {  
  3.             return NotFound();  
  4.         }  
NotFoundObjectResult
 
This is similar to NotFoundResult, with the difference being that you can pass an object with the 404 response.
  1. public NotFoundObjectResult NotFoundObjectActionResult()  
  2.         {  
  3.             return NotFound(new { Id = 1, error = "There was no customer with an id of 1." });  
  4.         }  
OkResult
 
This is a StatusCodeResult. When executed, it will produce an empty Status200OK response.
  1. public OkResult OkEmptyWithoutObject()  
  2. {  
  3.     return Ok();  
  4. }  
OkObjectResult
 
An ObjectResult, when executed, performs content negotiation, formats the entity body, and will produce a Status200OK response if negotiation and formatting succeed.
  1. public OkObjectResult OkObjectResult()  
  2.         {  
  3.             return new OkObjectResult(new { Message="Hello World !"});  
  4.         }  
NoContentResult
 
The action result returns 204 status code. It’s different from EmptyResult in that EmptyResult returns an empty 200 status code, but NoContentResult returns 204. Use EmptyResult in normal controllers and NoContentResult in API controllers.
  1. public NoContentResult NoContentActionResult()  
  2.         {  
  3.             return NoContent();  
  4.         }  
StatusCodeResult
 
StatusCodeResult accepts a status code number and sets that status code for the current request. One thing to point is that you can return an ObjectResult with and status code and object. There is a method on ControllerBase called StatusCode (404, new {Name = "Farhan Ahmed”}), which can take a status code and an object and return an ObjectResult.
  1. public StatusCodeResult StatusCodeActionResult()  
  2.         {  
  3.             return StatusCode(404);  
  4.         }