Specifying 4xx Details

I have a POST request that may cause a duplicate entry if it were to execute. As a response, I send back 409 Conflict to make clear what happened. However, I have some trouble adding some detail to explain what exactly happened. First, I tried the following, which is not RFC 9110 compliant because it returns a simple text/plain: return Conflict("A string explaining which property is already reserved"); When only returning Conflict();, the response body is application/problem+json as expected:
{
"type": "https://tools.ietf.org/html/rfc9110#section-15.5.10",
"title": "Conflict",
"status": 409,
"traceId": "uuid string"
}
{
"type": "https://tools.ietf.org/html/rfc9110#section-15.5.10",
"title": "Conflict",
"status": 409,
"traceId": "uuid string"
}
How can I add the "detail" property when returning Conflict() without altering the return type?
5 Replies
FusedQyou
FusedQyou5w ago
I do know .NET 9 has changes the way 4xx errors are supplied, in a way that satisfies modern RFC. Maybe look those up first
FusedQyou
FusedQyou5w ago
Nick Chapsas
YouTube
The Right Way To Return API Errors in .NET
Use code PATTERNS20 at checkout to get 20% off our 3 Design Patterns in C# Bundles: https://bit.ly/3YPiHLD Subscribe to my weekly newsletter: https://nickchapsas.com Become a Patreon and get special perks: https://www.patreon.com/nickchapsas Hello, everybody. I'm Nick, and in this video, I will show you the right or modern way of returning API...
Luca | LeCarbonator
Oh, interesting! I'll take a look. Also thanks for the video reference
Unknown User
Unknown User5w ago
Message Not Public
Sign In & Join Server To View
Luca | LeCarbonator
I haven‘t clarified it yet, you‘re right. My target is net8 maybe net9 soon if the changes are related to 4xx error handling as was mentioned before I‘m curious how the default formatting works if I don‘t pass anything to the function It doesn‘t seem editable on its own, but perhaps I can manually create it and then pass it as parameter
Want results from more Discord servers?
Add your server