ApiResponse<T>
Class public
A generic envelope for API responses, providing a consistent wire format that wraps the data payload alongside success/failure status and metadata.
Definition
Section titled Definitionpublic class ApiResponse<T>T Type Parameters
Section titled Type ParametersT The type of the data payload contained in the response.
Remarks
Section titled RemarksAll API endpoints should return
ApiResponse`1 to give clients a uniform structure for deserialization. The Success flag allows clients to branch on success or failure without inspecting HTTP status codes, while Timestamp provides server-side timing for debugging and cache-invalidation purposes. For error responses, Data will be null and Message will contain a human-readable error description. For richer error bodies, consider returning ProblemDetails as the T payload.Properties4
Section titled PropertiesT? T? ApiResponse<T>.DataGets or sets the response data payload.
Remarks
Contains the requested resource on success, or
null on failure.string? string? ApiResponse<T>.MessageGets or sets an optional human-readable message providing additional context about the response, such as a confirmation or an error description.
Success Section titled Success bool bool ApiResponse<T>.SuccessGets or sets a value indicating whether the request completed successfully.
Remarks
When
false, Message contains the error description and Data is null.Timestamp Section titled Timestamp DateTimeOffset DateTimeOffset ApiResponse<T>.TimestampGets or sets the UTC timestamp indicating when the server generated this response. Useful for debugging latency and cache invalidation.
Methods2
Section titled MethodsApiResponse<T> ApiResponse<T>.Fail( string message)Creates an error response with the specified message.
Parameters
message string The error message.
Returns
ApiResponse<T> A new
ApiResponse`1 marked as unsuccessful. ApiResponse<T> ApiResponse<T>.Ok( T data, string? message = null)Creates a successful response containing the specified data.
Parameters
data T The data payload.
message string? optional An optional message.
Returns
ApiResponse<T> A new
ApiResponse`1 marked as successful. Examples
Section titled ExamplesReturning a successful product response:
var product = new ProductDto { Id = Guid.NewGuid(), Name = "Widget" };var response = ApiResponse<ProductDto>.Ok(product, "Product retrieved");// { "data": { ... }, "message": "Product retrieved", "success": true, ... }Returning an error response:
var response = ApiResponse<ProductDto>.Fail("Product not found");// { "data": null, "message": "Product not found", "success": false, ... }