In this article I will explain with an example, how to return HTTP Status Code from Web API in ASP.Net Core.
This article will cover the in-built HTTP Status Codes i.e., 200, 201, 204, 400, 401, 403 and 404 as well as the ones for which there is no in-built function in ASP.Net Core.
 
 
HTTP Status Code 200 (HTTP Response OK)
HTTP Status Code 200 is used to return OK status i.e., request is completed and everything is fine.
Such HTTP Response it is returned using Ok function.
Optionally, you can also return an object along with the HTTP Response OK.
[Route("AjaxMethod")]
[HttpPost]
public IActionResult AjaxMethod(PersonModel person)
{
    return Ok();
}
 
 
HTTP Status Code 201 (HTTP Response Created)
HTTP Status Code 201 is used to return Created status i.e., when request is completed and a resource is created.
Such HTTP Response it is returned using Created function.
Optionally, you can also return, the URL where the object is created and also an object along with the HTTP Response Created.
[Route("AjaxMethod")]
[HttpPost]
public IActionResult AjaxMethod(PersonModel person)
{
    return Created();
}
 
 
HTTP Status Code 204 (HTTP Response No Content)
HTTP Status Code 204 is used to return NoContent status i.e., when request is completed and there is no requirement to redirect.
Such HTTP Response it is returned using NoContent function.
[Route("AjaxMethod")]
[HttpPost]
public IActionResult AjaxMethod(PersonModel person)
{
    return NoContent();
}
 
 
HTTP Status Code 400 (HTTP Response Bad Request)
HTTP Status Code 400 is used to return BadRequest status i.e., when request has error from client side and it cannot be processed.
Such HTTP Response it is returned using BadRequest function.
Optionally, you can also return, the Model State which has the error or also an object along with the HTTP Response BadRequest.
[Route("AjaxMethod")]
[HttpPost]
public IActionResult AjaxMethod(PersonModel person)
{
    return BadRequest();
}
 
 
HTTP Status Code 401 (HTTP Response Unauthorized)
HTTP Status Code 401 is used to return Unauthorized status i.e., when request has invalid or no credentials.
Such HTTP Response it is returned using Unauthorized function.
[Route("AjaxMethod")]
[HttpPost]
public IActionResult AjaxMethod(PersonModel person)
{
    return Unauthorized();
}
 
 
HTTP Status Code 403 (HTTP Response Forbidden)
HTTP Status Code 403 is used to return Forbidden status i.e., when Server understands the request but does not find it valid to be authorized.
Such HTTP Response it is returned using Forbid function.
Optionally, you can also return, the Authentication properties and Authentication schemes along with the HTTP Response Forbidden.
[Route("AjaxMethod")]
[HttpPost]
public IActionResult AjaxMethod(PersonModel person)
{
    return Forbid();
}
 
 
HTTP Status Code 404 (HTTP Response NotFound)
HTTP Status Code 404 is used to return NotFound status i.e., when request is done for a resource but it does not exists.
Such HTTP Response it is returned using NotFound function.
Optionally, you can also return an object along with the HTTP Response NotFound.
[Route("AjaxMethod")]
[HttpPost]
public IActionResult AjaxMethod(PersonModel person)
{
    return NotFound();
}
 
 
StatusCode function
The Status Code function is used to return any valid HTTP Response Status Code.
Optionally, you can also return an object along with the HTTP Response.
Direct Integer value
[Route("AjaxMethod")]
[HttpPost]
public IActionResult AjaxMethod(PersonModel person)
{
    return StatusCode(302);
}
 
Using StatusCodes Enum
[Route("AjaxMethod")]
[HttpPost]
public IActionResult AjaxMethod(PersonModel person)
{
    return StatusCode(Microsoft.AspNetCore.Http.StatusCodes.Status302Found);
}