AspNetCore Controller Return Types that Implement IActionResult

On my current project, we return Task<IActionResult> in the controller actions.  It makes it fairly painless to write useful unit tests.

asp net coreMiddleware Pipeline  – a common location for returning errors, like via SetForbiddenResponse()

Because I was having issues locating these in MSDN (not there, look in https://docs.microsoft.com/en-us/aspnet/core/api instead), here’s a list of valid types that implement IActionResult in AspNetCore (dotnet core):

  • BadRequestObjectResult
  • NotFoundObjectResult
  • ObjectResult (general handler)
  • OkObjectResult

This example returns a useful error to the consumer for debugging.  JSON might be better in your case.  There are properties on the object to tweak the response type.

return new ObjectResult("Something horrible just happened") { StatusCode = 500 };

Leave a Reply

Your email address will not be published. Required fields are marked *