Skip to main content

NotFoundResponse

Namespace: RA.Utilities.Api.Results

The NotFoundResponse class is a specialized model for creating standardized 404 Not Found responses. It is used when a requested resource cannot be found. It inherits from Response<T>, with the Result property typed as a NotFoundResult object.

🎯 Purpose

The NotFoundResponse class is a specialized model used to create standardized 404 Not Found API responses. It inherits from the base Response<T> class and is designed to clearly communicate when a requested resource does not exist.

Here's a breakdown of its key functions:

  1. Simplifies 404 Responses: It reduces boilerplate code by automatically setting the response properties for a "not found" scenario:
  • ResponseCode: Set to 404 (from BaseResponseCode.NotFound).
  • ResponseType: Set to ResponseType.NotFound.
  • ResponseMessage: Defaults to "The requested resource was not found." (from BaseResponseMessages.NotFound).
  1. Provides Structured Context: It uses a NotFoundResult object as its payload, containing the entity name and the identifier used in the search.

  2. Improves Client-Side Handling: This structured response allows clients to provide more intelligent feedback to the user.

⚙️ How It Works

When you create an instance of NotFoundResponse, it pre-configures the following properties:

  • ResponseCode: Set to 404 (from BaseResponseCode.NotFound).
  • ResponseType: Set to ResponseType.NotFound.
  • ResponseMessage: Defaults to "The requested resource was not found.".
  • Result: A NotFoundResult object containing the Entity name and Value that were searched for.

🚀 Usage in a Controller

using Microsoft.AspNetCore.Mvc;
using RA.Utilities.Api.Results;

[ApiController]
[Route("api/[controller]")]
public class ProductsController : ControllerBase
{
[HttpGet("{id}")]
public IActionResult GetProduct(int id)
{
var product = _productService.GetById(id);

if (product == null)
{
// Return a 404 Not Found with structured details
return NotFound(new NotFoundResponse(new NotFoundResult("Product", id)));
}

return Ok(new SuccessResponse<Product>(product));
}
}

NotFoundResult

Inherits from ErrorResult.

PropertyTypeDescription
EntitystringThe type of resource that was being looked for (e.g., "Product", "User").
ValueobjectThe identifier that was used in the search (e.g., 123, "john.doe@example.com").
ErrorCodestringThe specific error code (inherited from ErrorResult).
ErrorMessagestringThe error message (inherited from ErrorResult).

Example JSON Output

{
"responseCode": 404,
"responseType": "NotFound",
"responseMessage": "The requested resource was not found.",
"result": {
"entity": "Product",
"value": 999,
"errorCode": "NotFound",
"errorMessage": "The requested resource was not found."
}
}