SuccessResponse<T>
Namespace: RA.Utilities.Api.Results
The SuccessResponse<T> class is a specialized model for creating standardized, successful API responses.
It inherits from the base Response<T> and simplifies the process of returning a 200 OK result with a data payload.
🎯 Purpose
The SuccessResponse<T> class is a specialized, convenient wrapper for creating standardized successful API responses.
It inherits from the base Response<T> class and is designed to reduce boilerplate code in your controller actions.
Its primary purpose is to simplify the creation of a 200 OK response by pre-configuring the standard response properties for you:
- Sets
ResponseCodeto 200: It automatically sets theResponseCodeto200(usingBaseResponseCode.Success). - Sets
ResponseTypetoSuccess: It sets theResponseTypeenum toResponseType.Success. - Provides a Default Message: It assigns a default
ResponseMessageof"Operation completed successfully."(fromBaseResponseMessages.Success), which you can override if needed. - Accepts the Payload: Its constructor takes the data payload (
T) that you want to send back to the client.
⚙️ How It Works
When you create an instance of SuccessResponse<T>, it pre-configures the following properties:
ResponseCode: Set to200(fromBaseResponseCode.Success).ResponseType: Set toResponseType.Success.ResponseMessage: Defaults to"Operation completed successfully."(fromBaseResponseMessages.Success), but can be overridden via the constructor.Result: Set to the data payload you provide.
🚀 Usage in a Controller
You can use this class directly in your controller actions to wrap your data and return a standardized OkObjectResult (HTTP 200).
using Microsoft.AspNetCore.Mvc;
using RA.Utilities.Api.Results;
[ApiController]
[Route("api/[controller]")]
public class ProductsController : ControllerBase
{
[HttpGet("{id}")]
public IActionResult GetProduct(int id)
{
// Assume product is fetched from a service
var product = new Product { Id = id, Name = "Sample Product" };
return Ok(new SuccessResponse<Product>(product));
}
}
Example JSON Output
The code above would produce the following JSON response body:
{
"responseCode": 200,
"responseType": "Success",
"responseMessage": "Operation completed successfully.",
"result": {
"id": 1,
"name": "Sample Product"
}
}