Skip to main content

ConflictResponse

Namespace: RA.Utilities.Api.Response

The ConflictResponse class creates a standardized 409 Conflict response. It derives from Response<T> with T = ConflictResult, so Result identifies the entity and value that caused the conflict.

🎯 Purpose​

Use ConflictResponse when a request cannot be completed because it conflicts with existing state — for example, creating a user whose email address is already registered.

  • Standard envelope: ResponseCode = 409, ResponseType = ResponseType.Conflict, ResponseMessage = "A conflict occurred with the current state of the resource."
  • Typed payload: Result is a ConflictResult with Entity and Value.
  • Customizable: Override the envelope code/message or the nested error code/message.

Constructors​

ConstructorResultResponseCodeResponseMessage
ConflictResponse(ConflictResult model)model409"A conflict occurred with the current state of the resource."
ConflictResponse(ConflictResult model, int responseCode, string responseMessage)modelresponseCoderesponseMessage

ResponseType is always ResponseType.Conflict (JSON 9).

ConflictResult​

ConflictResult inherits from ErrorResult.

PropertyTypeDescription
EntitystringThe resource type that caused the conflict (e.g., "User").
ValueobjectThe value that caused the conflict (e.g., "test@example.com").
ErrorCodestring?Machine-readable code (inherited). Defaults to null and is omitted from JSON.
ErrorMessagestringHuman-readable message (inherited). Defaults to an empty string, not the standard envelope message.

Constructor:

public ConflictResult(
string entity,
object value,
string? errorCode = null,
string errorMessage = "")
Nested message defaults to empty

new ConflictResult("User", "a@b.com") leaves ErrorMessage empty. The ConflictResponse(ConflictResult) convenience constructor only sets the envelope message. Set the nested message explicitly when clients need it:

var conflict = new ConflictResult("User", request.Email)
{
ErrorCode = "EMAIL_ALREADY_EXISTS",
ErrorMessage = "A user with this email address already exists."
};

Usage​

using Microsoft.AspNetCore.Mvc;
using RA.Utilities.Api.Response;

[ApiController]
[Route("api/[controller]")]
public class UsersController : ControllerBase
{
[HttpPost]
public IActionResult CreateUser(CreateUserRequest request)
{
if (_userService.EmailExists(request.Email))
{
var conflict = new ConflictResult("User", request.Email)
{
ErrorCode = "EMAIL_ALREADY_EXISTS",
ErrorMessage = $"A user with email '{request.Email}' already exists."
};

return Conflict(new ConflictResponse(conflict));
}

// ... create the user
return Ok(new SuccessResponse<User>(user));
}
}

Example JSON Output​

{
"responseCode": 409,
"responseType": 9,
"responseMessage": "A conflict occurred with the current state of the resource.",
"result": {
"entity": "User",
"value": "existing.user@example.com",
"errorCode": "EMAIL_ALREADY_EXISTS",
"errorMessage": "A user with email 'existing.user@example.com' already exists."
}
}

With JsonStringEnumConverter, responseType is "Conflict".