How to return a specific status code and no contents from Controller in C#?

How to return a specific status code and no contents from Controller in C#?

To return a specific status code and no content from a controller action in C#, you can use the StatusCodeResult class provided by ASP.NET Core.

Here's an example:

 [HttpDelete("{id}")] public IActionResult Delete(int id) { // your deletion logic here if (/* deletion is successful */) { return new StatusCodeResult(StatusCodes.Status204NoContent); } else { return new StatusCodeResult(StatusCodes.Status500InternalServerError); } } 

In this example, the Delete action returns a 204 No Content status code when the deletion is successful, and a 500 Internal Server Error status code when there's an error.

Examples

  1. "ASP.NET Core return 204 No Content from Controller"

    • Code:
      [ApiController] [Route("api/example")] public class ExampleController : ControllerBase { [HttpDelete("{id}")] public IActionResult DeleteResource(int id) { // Delete resource logic if (resourceNotFound) { return NotFound(); // 404 Not Found } // Successful deletion, return 204 No Content return NoContent(); } } 
    • Description: This example demonstrates how to return a 204 No Content status code from a DELETE action in an ASP.NET Core controller.
  2. "C# Web API return 202 Accepted with no content"

    • Code:
      [ApiController] [Route("api/example")] public class ExampleController : ControllerBase { [HttpPut("{id}")] public IActionResult UpdateResource(int id, [FromBody] UpdateModel model) { // Update resource logic if (resourceNotFound) { return NotFound(); // 404 Not Found } // Successful update, return 202 Accepted with no content return Accepted(); } } 
    • Description: This code snippet illustrates how to return a 202 Accepted status code with no content from a PUT action in a Web API controller.
  3. "ASP.NET Core return 404 Not Found with no content"

    • Code:
      [ApiController] [Route("api/example")] public class ExampleController : ControllerBase { [HttpGet("{id}")] public IActionResult GetResource(int id) { // Get resource logic if (resourceNotFound) { return NotFound(); // 404 Not Found with no content } // Return resource return Ok(resource); } } 
    • Description: This example showcases how to return a 404 Not Found status code with no content when a resource is not found in an ASP.NET Core controller.
  4. "C# Web API return 400 Bad Request with no content"

    • Code:
      [ApiController] [Route("api/example")] public class ExampleController : ControllerBase { [HttpPost] public IActionResult CreateResource([FromBody] CreateModel model) { // Validation logic if (validationFailed) { return BadRequest(); // 400 Bad Request with no content } // Create resource logic return CreatedAtAction(nameof(GetResource), new { id = createdResourceId }, null); } } 
    • Description: This code snippet demonstrates how to return a 400 Bad Request status code with no content when input validation fails in a Web API controller.
  5. "ASP.NET Core return 201 Created with no content"

    • Code:
      [ApiController] [Route("api/example")] public class ExampleController : ControllerBase { [HttpPost] public IActionResult CreateResource([FromBody] CreateModel model) { // Create resource logic if (resourceAlreadyExists) { return Conflict(); // 409 Conflict } // Successful creation, return 201 Created with no content return CreatedAtAction(nameof(GetResource), new { id = createdResourceId }); } } 
    • Description: This example illustrates how to return a 201 Created status code with no content upon successfully creating a resource in an ASP.NET Core controller.
  6. "C# Web API return 403 Forbidden with no content"

    • Code:
      [ApiController] [Route("api/example")] public class ExampleController : ControllerBase { [HttpGet("{id}")] public IActionResult GetResource(int id) { // Authorization logic if (userNotAuthorized) { return Forbid(); // 403 Forbidden with no content } // Return resource return Ok(resource); } } 
    • Description: This code snippet demonstrates how to return a 403 Forbidden status code with no content when a user is not authorized to access a resource in a Web API controller.
  7. "ASP.NET Core return 412 Precondition Failed with no content"

    • Code:
      [ApiController] [Route("api/example")] public class ExampleController : ControllerBase { [HttpPatch("{id}")] public IActionResult UpdateResource(int id, [FromBody] UpdateModel model) { // Precondition check if (preconditionFailed) { return StatusCode(412); // 412 Precondition Failed with no content } // Update resource logic return Ok(updatedResource); } } 
    • Description: This example shows how to return a 412 Precondition Failed status code with no content when a precondition for an update is not met in an ASP.NET Core controller.
  8. "C# Web API return 500 Internal Server Error with no content"

    • Code:
      [ApiController] [Route("api/example")] public class ExampleController : ControllerBase { [HttpGet("{id}")] public IActionResult GetResource(int id) { // Internal server error handling if (internalServerError) { return StatusCode(500); // 500 Internal Server Error with no content } // Return resource return Ok(resource); } } 
    • Description: This code snippet illustrates how to return a 500 Internal Server Error status code with no content when an unexpected error occurs in a Web API controller.
  9. "ASP.NET Core return 409 Conflict with no content"

    • Code:
      [ApiController] [Route("api/example")] public class ExampleController : ControllerBase { [HttpDelete("{id}")] public IActionResult DeleteResource(int id) { // Delete resource logic if (resourceInUse) { return Conflict(); // 409 Conflict with no content } // Successful deletion, return 204 No Content return NoContent(); } } 
    • Description: This example demonstrates how to return a 409 Conflict status code with no content when a resource is in use and cannot be deleted in an ASP.NET Core controller.
  10. "C# Web API return custom status code with no content"

    • Code:
      [ApiController] [Route("api/example")] public class ExampleController : ControllerBase { [HttpGet("{id}")] public IActionResult GetResource(int id) { // Custom logic if (customCondition) { return StatusCode(418); // Custom status code (e.g., 418 I'm a teapot) with no content } // Return resource return Ok(resource); } } 
    • Description: This code snippet showcases how to return a custom status code (e.g., 418 I'm a teapot) with no content from a Web API controller in C#.

More Tags

drive google-oauth sudo create-guten-block shadow proto first-responder applicationpoolidentity wakelock udev

More C# Questions

More Trees & Forestry Calculators

More Tax and Salary Calculators

More Genetics Calculators

More Transportation Calculators