ConflictResponse
Namespace: RA.Utilities.Api.Results
The ConflictResponse class is a specialized model for creating standardized 409 Conflict responses.
It is used when a request cannot be completed because it conflicts with the current state of a resource, such as attempting to create a resource that already exists.
It inherits from Response<T>, with the Result property typed as a ConflictResult object.
🎯 Purpose
The ConflictResponse class is a specialized model for creating standardized 409 Conflict API responses.
It inherits from the base Response<T> class and is used to signal that a request could not be completed because it conflicts with the current state of a resource.
Here are its primary functions:
-
Standardizes Conflict Errors: It provides a consistent structure for all
409 Conflicterrors. -
Reduces Boilerplate: It automatically sets the response properties for a conflict scenario:
- ResponseCode: Set to
409(fromBaseResponseCode.Conflict). - ResponseType: Set to
ResponseType.Conflict. - ResponseMessage: Defaults to
"The request could not be completed due to a conflict with the current state of the resource."(fromBaseResponseMessages.Conflict).
- Provides Structured Context: The response payload is a
ConflictResultobject, which contains the entity name and the value that caused the conflict.
⚙️ How It Works
When you create an instance of ConflictResponse, it pre-configures the following properties:
ResponseCode: Set to409(fromBaseResponseCode.Conflict).ResponseType: Set toResponseType.Conflict.ResponseMessage: Defaults to the standard conflict message.Result: AConflictResultobject containing theEntityname and conflictingValue.
🚀 Usage in a Controller
using Microsoft.AspNetCore.Mvc;
using RA.Utilities.Api.Results;
[ApiController]
[Route("api/[controller]")]
public class UsersController : ControllerBase
{
[HttpPost]
public IActionResult CreateUser(CreateUserRequest request)
{
if (_userService.EmailExists(request.Email))
{
// Return a 409 Conflict with structured details
return Conflict(new ConflictResponse(new ConflictResult("User", request.Email)));
}
// ... proceed with user creation
return Ok();
}
}
ConflictResult
Inherits from ErrorResult.
| Property | Type | Description |
|---|---|---|
| Entity | string | The type of resource causing the conflict (e.g., "User"). |
| Value | object | The specific value that caused the conflict (e.g., "test@example.com"). |
| ErrorCode | string | The specific error code (inherited from ErrorResult). |
| ErrorMessage | string | The error message (inherited from ErrorResult). |
Example JSON Output
{
"responseCode": 409,
"responseType": "Conflict",
"responseMessage": "The request could not be completed due to a conflict with the current state of the resource.",
"result": {
"entity": "User",
"value": "existing.user@example.com",
"errorCode": "Conflict",
"errorMessage": "The request could not be completed due to a conflict with the current state of the resource."
}
}