Return NotFound with details

Hello. I have a asp.net core Web API with controllers and I want to return for example a NotFound Error with a custom message attached, that I can then show to the user.
[HttpGet("{id:guid}", Name = "GetUrlaubsantragById")]
[ProducesResponseType(typeof(UrlaubsantragDto), StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
[ProducesResponseType(StatusCodes.Status403Forbidden)]
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
public async Task<IActionResult> GetUrlaubsantragById([FromRoute] Guid id)
{
var result = await _urlaubsantragService.GetUrlaubsantragAsync(id);
if (result == null)
{
return NotFound();
}

return Ok(result);
}
[HttpGet("{id:guid}", Name = "GetUrlaubsantragById")]
[ProducesResponseType(typeof(UrlaubsantragDto), StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
[ProducesResponseType(StatusCodes.Status403Forbidden)]
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
public async Task<IActionResult> GetUrlaubsantragById([FromRoute] Guid id)
{
var result = await _urlaubsantragService.GetUrlaubsantragAsync(id);
if (result == null)
{
return NotFound();
}

return Ok(result);
}
This for example creates a problem, so the format that I want:
{
"type": "https://tools.ietf.org/html/rfc7231#section-6.5.4",
"title": "Not Found",
"status": 404,
"traceId": "00-cb37e3ba34b597ba19e7cf39665feb58-73e25a81a8b6a327-00"
}
{
"type": "https://tools.ietf.org/html/rfc7231#section-6.5.4",
"title": "Not Found",
"status": 404,
"traceId": "00-cb37e3ba34b597ba19e7cf39665feb58-73e25a81a8b6a327-00"
}
But when I add a message to the ActionResult I only get a text/plain message:
public async Task<IActionResult> GetUrlaubsantragById([FromRoute] Guid id)
{
var result = await _urlaubsantragService.GetUrlaubsantragAsync(id);
if (result == null)
{
return NotFound("My custom error message");
}

return Ok(result);
}
public async Task<IActionResult> GetUrlaubsantragById([FromRoute] Guid id)
{
var result = await _urlaubsantragService.GetUrlaubsantragAsync(id);
if (result == null)
{
return NotFound("My custom error message");
}

return Ok(result);
}
Does someone have an idea, what is going on?
8 Replies
JakenVeina
JakenVeina2y ago
what's going on is that you told your API method to return a string, so that's what it did
Jan Krüger
Jan KrügerOP2y ago
Okay, but is there a way to convert that into a problem details?
Jan Krüger
Jan KrügerOP2y ago
So I'd have to create an instance of that and then return that? But it feels like a lot of boiler code to add that at every controller and for every possible response code. I just wanted a consistent error response format to for all errors, with optional details
Jan Krüger
Jan KrügerOP2y ago
Thank you for the resources, I'll take a look at that 🙂
JakenVeina
JakenVeina2y ago
I think some of that latter section, with regards to services.AddProblemDetails() is only applicable to ASP.NET Core 7+. if thay matters on your end
Jan Krüger
Jan KrügerOP2y ago
That is fine, tho I see this:
The following code configures the app to generate a problem details response for all HTTP client and server error responses that don't have a body content yet:
Do have an Idea how I could still attach details? I could go ahead and create exceptions for each status code, I guess. But this still feels like a lot of boilerplate to me. And its imho something very basic so I thought there might a already build solution for this
Want results from more Discord servers?
Add your server