swagger 使用Swashbuckle为401错误代码设置不同的媒体类型

toe95027  于 2022-12-04  发布在  其他
关注(0)|答案(1)|浏览(144)

在Swagger UI中,是否可以配置Swashbuckle为特定HTTP错误代码401生成不同的媒体类型?

我希望400错误代码的媒体类型是application/JSON,但对于401,它是text/HTML。有什么方法可以实现这一点吗?
控制器上方注解,

[Authorize]
[ApiController]
[ApiVersion("1.0")]
[Route("[controller]")]
[Route("v{version:apiVersion}/[controller]")]
[Produces("application/json")]
public class MyController : ControllerBase
{
}

我的方法上面的注解,

[ProducesResponseType(StatusCodes.Status201Created)]
    [ProducesResponseType(StatusCodes.Status400BadRequest)]
    [ProducesResponseType(StatusCodes.Status401Unauthorized)]
    [ProducesResponseType(StatusCodes.Status500InternalServerError)]
    [HttpPost]
    public async Task<ActionResult<MyModel>> PostAsync(MyRequest myRequest)
    {
}
92vpleto

92vpleto1#

您是否尝试过SwaggerResponse属性,它派生自ProducesResponseType,并已增强为支持添加内容媒体类型:

[SwaggerResponse(StatusCodes.Status201Created, "Successfully created something", typeof(MyModel), "application/json")]
    [SwaggerResponse(StatusCodes.Status401Unauthorized, "Unauthorized", typeof(string), "text/html")]
    [SwaggerResponse(StatusCodes.Status400BadRequest, "Invalid request", typeof(string), "text/html")]
    [SwaggerResponse(StatusCodes.Status500InternalServerError, "Internal Server Error", typeof(string), "text/html")]

它是核心包的一部分:Swashbuckle.AspNetCore.Annotations

相关问题