I want to render a ProblemDetails (Microsoft.AspNetCore.Mvc) response body from a NoContent() response.
{
“type”: “string”,
“title”: “string”,
“status”: 0,
“detail”: “string”,
“instance”: “string”
}
According to docs, ProblemDetails, ProblemDetails will not automatically render from any status code less than 400.
I have played around with modifying Program.cs with builder.services.AddProblemDetails, ProblemDetailsFactory, etc. (see documentation link above)
No matter what I do, I’ve not had luck with 204.
Has anyone done this successfully, can it be done? Any pointers in the right direction?
namespace Company.Controllers
{
[ApiController]
[Route("[controller]")]
public class ImportantController : BaseController
{
[HttpGet]
[ValidateModelState]
[Route("/api/important")]
[Produces("application/json")]
[ProducesResponseType(typeof(IEnumerable<ImportantDataResponse>), 200)]
[ProducesResponseType(typeof(ProblemDetails), StatusCodes.Status204NoContent)]
[ProducesResponseType(typeof(ProblemDetails), StatusCodes.Status400BadRequest)]
public async Task<IActionResult> GetImportantData([FromQuery][Required] string dashboardId)
{
...
try
{
IEnumerable<ImportantDataModel> objResult = await _circuitBreakerPolicy.ExecuteAsync(() => _service.GetImportantData(dashboardId));
if (objResult.Count() > 0)
{
return Ok(objResult);
} else
{
return NoContent();
}
}
catch (Exception ex) {
throw;
}
return BadRequest();
}