Skip to content

3. Extra response properties

Stratis Dermanoutsos edited this page Sep 18, 2025 · 5 revisions

Let's say that we want to throw a conflict error.

1. Default behaviour

That would be as simple as throwing an error:

throw new ConflictRestException();

The result will be the expected:

{
  "type": "https://www.rfc-editor.org/rfc/rfc9110.html#name-409-conflict",
  "title": "Conflict",
  "status": 409,
  "detail": "Concurrent tasks disallowed.",
  "instance": "/error/409",
  "traceId": "00-5a12a5d4478ab3e048a88e4b104633db-af7677b367ae81ba-00",
  "method": "GET",
  "requestId": "0HNE94H45RD5P:00000001"
}

2. Add extra properties

To achieve this, we utilize the ProblemDetails.Extensions property.

throw new ConflictRestException(extensions: new Dictionary<string, object?>
{
    { "conflictProperty", "This is a Conflict prop." },
    { "conflictProperty2", "This is another Conflict prop." },
});

Each of the Dictionary's values can be a whole other object or null.

! NOTE that null values are ignored.

The new result will be:

{
  "type": "https://www.rfc-editor.org/rfc/rfc9110.html#name-409-conflict",
  "title": "Conflict",
  "status": 409,
  "detail": "Concurrent tasks disallowed.",
  "instance": "/error/409",
  "conflictProperty": "This is a Conflict prop.",
  "conflictProperty2": "This is another Conflict prop.",
  "traceId": "00-5a12a5d4478ab3e048a88e4b104633db-af7677b367ae81ba-00",
  "method": "GET",
  "requestId": "0HNE94H45RD5P:00000001"
}

3. Disclaimer

This adds the extra properties to this specific response.

For adding more default properties to all error responses, see Override ProblemDetails builder.

Clone this wiki locally