Response<T>
Namespace: RA.Utilities.Api.Response
The Response<T> class is the universal wrapper for all API responses in the RA.Utilities ecosystem. It defines the JSON envelope shared by every response model: responseCode, responseType, responseMessage, and result.
🎯 Purpose
Response<T> is the base type for the package. It gives every success and failure response the same shape, so clients can parse the envelope once and then inspect the Result payload.
- Standardization: Every response carries the same four properties.
- Predictability: Clients can branch on
ResponseCodeorResponseTypebefore readingResult. - Extensibility: The package's response classes derive from
Response<T>; you can derive from it too when you need a status code the package does not wrap. - Immutability: All properties use
initaccessors, so a response cannot be mutated after construction.
Class Definition
public class Response<T>
{
public int ResponseCode { get; init; }
public ResponseType ResponseType { get; init; }
public string? ResponseMessage { get; init; }
public T? Result { get; init; }
}
Properties
| Property | Type | Description |
|---|---|---|
| ResponseCode | int | The response code, typically an HTTP status code but allowed to be a domain-specific code. |
| ResponseType | ResponseType | The semantic outcome (Success, BadRequest, NotFound, ...) from RA.Utilities.Core.Constants. Serialized as a number by default; see JSON serialization. |
| ResponseMessage | string? | A human-readable message describing the outcome. |
| Result | T? | The payload. null for responses that carry no data. |
Because all four properties are init-only, object initializers work, but mutation after construction does not:
var response = new Response<string>
{
ResponseCode = BaseResponseCode.Success,
ResponseType = ResponseType.Success,
ResponseMessage = "Operation completed successfully.",
Result = "payload"
};
JSON Serialization
ASP.NET Core serializes properties in camelCase by default. ResponseType is an enum, so System.Text.Json writes its numeric value unless a string-enum converter is registered.
{
"responseCode": 200,
"responseType": 0,
"responseMessage": "Operation completed successfully.",
"result": {
"id": 123,
"name": "Example Product"
}
}
To write "Success" instead of 0, register JsonStringEnumConverter:
using System.Text.Json.Serialization;
builder.Services.ConfigureHttpJsonOptions(options =>
{
options.SerializerOptions.Converters.Add(new JsonStringEnumConverter());
});
For the full converter behavior and the numeric value of every member, see ResponseType.
Derived Types
The package ships these derived response classes:
| Type | HTTP | Result type |
|---|---|---|
SuccessResponse<T> | 200 | T |
BadRequestResponse | 400 | BadRequestResult[] |
UnauthorizedResponse | 401 | ErrorResult |
PaymentRequiredResponse | 402 | ErrorResult |
ForbiddenResponse | 403 | ErrorResult |
NotFoundResponse | 404 | NotFoundResult |
ConflictResponse | 409 | ConflictResult |
UnprocessableResponse | 422 | ErrorResult |
TooManyRequestsResponse | 429 | ErrorResult |
ErrorResponse | 500 | ErrorResult |
ServiceUnavailableResponse | 503 | ErrorResult |
GatewayTimeoutResponse | 504 | ErrorResult |
Response<T> has an implicit public parameterless constructor, so you can also instantiate it directly for a one-off response. Prefer a derived type when one matches your status code.