I have a simple .NET 6 API that is using Swashbuckle (6.3.1).
I've noticed that when I do a request from Swagger UI (after hitting F5 in VS2022) and when I return error details they are not displayed in Swagger UI, but when the same request is done in a new chrome tab the error details are returned, same with Postman.
when I do the same request in another tab I get this (correct) response:
I noticed that Swagger UI is doing a fetch type request, but when I do a "normal" request I get a document type request:
My question is how can I configure Swashbuckle so that I get error details when I do a request from Swagger UI.
As requested I'm adding my controller:
[ApiController]
public class LocalizationController : ApiController
{
/// <summary>
/// Get supported languages
/// </summary>
/// <param name="model"></param>
/// <returns></returns>
[HttpGet("languages")]
[AllowAnonymous]
[Cache(CacheDuration.FiveMinutes, 1, CacheTags.Languages)]
public async Task<IEnumerable<LanguageDto>> GetLanguages([FromRoute] GetLanguages model) => await Process(model);
/// <summary>
/// Get localized strings in requested language
/// </summary>
/// <param name="model"></param>
/// <returns></returns>
[HttpGet("{language}")]
[ProducesResponseType(typeof(IEnumerable<LocalizationDto>), StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
[AllowAnonymous]
[Cache(CacheDuration.FiveMinutes, 1, CacheTags.Languages)]
public async Task<IEnumerable<LocalizationDto>> GetLocalizations([FromRoute] GetLocalizations model) => await Process(model);
}
and my base ApiController:
public abstract class ApiController : ControllerBase
{
/// <summary>
/// MediatR mediator
/// </summary>
protected IMediator Mediator => HttpContext.RequestServices.GetRequiredService<IMediator>();
/// <summary>
/// Maps the given object to the given type
/// </summary>
protected IMapper Mapper => HttpContext.RequestServices.GetRequiredService<IMapper>();
protected async Task<TResponse> Process<TResponse>(ICommand<TResponse> command)
{
return await Mediator.Send(command);
}
/// <summary>
/// Process command - Create, Update, Delete
/// </summary>
/// <param name="command"></param>
/// <typeparam name="TModel"></typeparam>
/// <typeparam name="TResponse"></typeparam>
/// <returns></returns>
protected async Task<TResponse> Process<TModel, TResponse>(ICommand<TModel> command)
{
var result = await Mediator.Send(command);
return Mapper.Map<TResponse>(result);
}
/// <summary>
/// Process query - only Read
/// </summary>
/// <param name="query"></param>
/// <typeparam name="TResponse"></typeparam>
/// <returns></returns>
protected async Task<TResponse> Process<TResponse>(IQuery<TResponse> query)
{
return await Mediator.Send(query);
}
}