Controller response helper methods

The ASP.NET Core ControllerBase base class, from which all the controllers inherit, provides a few helper methods for creating responses instead of manually creating and setting instances of IActionResult derived types. Here is a short list of some of the popular ones:

  • Ok(...): Creates an OkResult object with or without content
  • StatusCode(int statusCode): Creates a response with a given status code
  • BadRequest(...): Creates a BadRequestObjectResult with a 400 status code and a body 

For a complete list, refer to the ControllerBase documentation, available at https://docs.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.mvc.controllerbase?view=aspnetcore-2.0#Methods.

Get Hands-On Full-Stack Web Development with ASP.NET Core now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.