Senger CodeLab 🚀

How to return a specific status code and no contents from Controller

September 29, 2026

How to return a specific status code and no contents from Controller

In the intricate world of web API development, crafting robust and efficient endpoints is paramount. Developers often encounter scenarios where a server-side operation completes successfully, but there’s no specific data to return to the client. Perhaps a record was deleted, an update was processed, or a background task was initiated. In such cases, simply returning an empty response might lead to ambiguity for the client, which expects a clear signal of success or failure. This is precisely where understanding how to return a specific status code and no contents from Controller becomes crucial. It’s about communicating effectively with the client without unnecessary data transfer, adhering to HTTP standards, and ensuring your APIs are both lean and explicit.

Understanding HTTP Status Codes and the “No Content” Concept

HTTP status codes are fundamental to how web servers communicate the outcome of a client’s request. These three-digit numbers are standardized, providing immediate insight into whether a request was successful, redirected, experienced a client-side error, or encountered a server-side issue. While codes like 200 OK (indicating success with content) are common, equally important are codes that signify success without the need for a response body.

The 204 No Content status code is a prime example. It indicates that the server has successfully fulfilled the request, but there is no new content to send back in the response body. This is particularly useful for operations like DELETE requests, where the resource has been successfully removed, or PUT requests where a resource has been updated and the client already possesses the necessary information. Using 204 explicitly informs the client that the operation was successful and that it should not expect any data. This distinction is vital for client-side applications to correctly interpret the server’s response and avoid unnecessary parsing of an empty body.

Beyond 204, other status codes can also convey success without content, depending on the context. For instance, 202 Accepted indicates that the request has been accepted for processing, but the processing is not yet complete. This is often used for asynchronous operations where the server will eventually process the request but doesn’t have an immediate result to return. Conversely, a 404 Not Found or 400 Bad Request might also return no content, although these signify errors rather than successful operations, directing the client to halt processing or try again. Properly utilizing these codes is a cornerstone of effective RESTful API design.

To return a specific status code and no contents from Controller, developers typically leverage built-in framework features. For instance, in ASP.NET Core, an OkResult or NoContentResult can be returned directly, while in Spring Boot, an ResponseEntity can be configured with the desired status and an empty body. These mechanisms abstract away the low-level HTTP protocol details, allowing developers to focus on the business logic while ensuring correct API behavior. Adhering to these standards improves interoperability and maintainability of web services.

Practical Implementations Across Frameworks

Implementing a “no content” response with a specific status code varies slightly across different web development frameworks, but the underlying principle remains consistent: signal success or a specific state without sending a response body. Here’s how commonly used frameworks approach this, illustrating how to return a specific status code and no contents from Controller effectively.

ASP.NET Core Controllers

In ASP.NET Core, the framework provides intuitive ways to return various HTTP responses. To return a 204 No Content, you simply use the NoContent() helper method within your controller action. For other status codes without content, you can use StatusCode(int statusCode). This simplicity is a hallmark of ASP.NET Core’s API design, allowing developers to quickly convey the intended response.

For example, if you have a DELETE endpoint for removing a resource, your controller action might look like this conceptually:

public IActionResult DeleteResource(int id) { var success = _resourceService.Delete(id); if (success) { return NoContent(); // Returns 204 No Content } return NotFound(); // Returns 404 Not Found } 

This approach keeps the code clean and expressive, ensuring clients receive the appropriate HTTP signals. As noted by Microsoft documentation on ASP.NET Core web APIs, using specific IActionResult types like NoContentResult or StatusCodeResult is the recommended practice for explicit and clear API responses.

Spring Boot Controllers

Spring Boot, based on Spring Framework, offers the ResponseEntity class to provide full control over the HTTP response, including status codes, headers, and body. To send a specific status code without a body, you can construct a ResponseEntity with an appropriate HttpStatus.

@DeleteMapping("/resources/{id}") public ResponseEntity<Void> deleteResource(@PathVariable Long id) { boolean deleted = resourceService.delete(id); if (deleted) { return ResponseEntity.noContent().build(); // Returns 204 No Content } return ResponseEntity.notFound().build(); // Returns 404 Not Found } 

The <Void> generic type in ResponseEntity explicitly indicates that no response body is expected, reinforcing the API contract. This method aligns with the typical Spring approach of providing robust and flexible tools for web API development.

Node.js with Express.js

Express.js, a popular framework for Node.js, provides direct methods on the response object to set status codes and end the response without sending a body. The res.status(statusCode).end() pattern is commonly used.

app.delete('/api/resources/:id', (req, res) => { const deleted = resourceService.delete(req.params.id); if (deleted) { res.status(204).end(); // Returns 204 No Content } else { res.status(404).end(); // Returns 404 Not Found } }); 

The .end() method signals that the response has been completed and no more data should be sent. This direct manipulation of the response object offers fine-grained control, which is characteristic of Node.js environments.

Infographic here
Best Practices for API Design **Question & Answer :**

I want the example controller below to return a status code 418 with no contents. Setting the status code is easy enough but then it seems like there is something that needs to be done to signal the end of the request. In MVC prior to ASP.NET Core or in WebForms that might be a call to Response.End() but how does it work in ASP.NET Core where Response.End does not exist?

public class ExampleController : Controller { [HttpGet][Route("/example/main")] public IActionResult Main() { this.HttpContext.Response.StatusCode = 418; // I'm a teapot // How to end the request? // I don't actually want to return a view but perhaps the next // line is required anyway? return View(); } } 

this.HttpContext.Response.StatusCode = 418; // I'm a teapot

How to end the request?

Try other solution, just:

return StatusCode(418); 

You could use StatusCode(???) to return any HTTP status code.

Also, you can use dedicated results:

Success:

  • return Ok() ← Http status code 200
  • return Created() ← Http status code 201
  • return NoContent(); ← Http status code 204

Client Error:

  • return BadRequest(); ← Http status code 400
  • return Unauthorized(); ← Http status code 401
  • return NotFound(); ← Http status code 404

More details: