Skip to content

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.
Namespace SampleApi
Assembly SampleApi
Package v1.0.0.0
public class ApiResponse<T>
T
T
The type of the data payload contained in the response.
All 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.
Data Section titled Data nullable
T?
T? ApiResponse<T>.Data

Gets or sets the response data payload.

Contains the requested resource on success, or null on failure.
Message Section titled Message nullable
string?
string? ApiResponse<T>.Message

Gets or sets an optional human-readable message providing additional context about the response, such as a confirmation or an error description.

bool ApiResponse<T>.Success

Gets or sets a value indicating whether the request completed successfully.

When false, Message contains the error description and Data is null.
DateTimeOffset
DateTimeOffset ApiResponse<T>.Timestamp

Gets or sets the UTC timestamp indicating when the server generated this response. Useful for debugging latency and cache invalidation.

ApiResponse<T> ApiResponse<T>.Fail(
string message)

Creates an error response with the specified message.

message string
The error message.
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.

data T
The data payload.
message string? optional
An optional message.
ApiResponse<T> A new ApiResponse`1 marked as successful.

Returning 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, ... }