如何使用Swashbuckle设置“参数内容类型”?

时间:2019-05-03 23:11:06

标签: swagger swagger-ui swashbuckle asp.net-core-2.2

我的招摇用户界面显示“参数内容类型”,其中包含各种条目:let date = new Date(); let order = ['weekday', 'day']; new Intl.DateTimeFormat('en-US', { weekday: 'short', day: 'numeric' }).formatToParts(date).filter((elem) => { return (elem.type !== 'literal'); }).sort((a, b) => { // SET ORDER let i = 0; a.index = -1; b.index = -1; order.forEach((type) => { if (a.type === type) { a.index = i; } if (b.type === type) { b.index = i; } i++; }); return (a.index - b.index); }).map(({type, value}) => { // MODIFY ELEMENT switch (type) { default : return (value); } }).reduce((string, part) => string + ' ' + part); "application/json-patch+json""text/json""application/json"

我只想要"application/*+json"

回购协议中有一个similar未解决的问题,它使用以下视觉效果(旧版ui,但思路相同):

enter image description here

有什么设置方法吗?

"application/json"版本Swashbuckle.AspNetCore
4.0.1Swashbuckle.AspNetCore.Filters

1 个答案:

答案 0 :(得分:2)

使用[Produces][Consumes]属性。 Swashbuckle(以及其他类似NSwag的对象)会将其转换为适当的Swagger文档。

[Consumes]属性的构造函数的第一个参数是String contentType

public ConsumesAttribute ( string contentType, params string[] otherContentTypes );

https://docs.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.mvc.consumesattribute.-ctor?view=aspnetcore-2.2#Microsoft_AspNetCore_Mvc_ConsumesAttribute__ctor_System_String_System_String___

像这样:

[ApiController]
public class MyController : ControllBase
{
    [HttpPost( "/foo/bar" )]
    [Consumes( "application/json" )]
    [Produces( typeof(MyResponseDto) )
    public async Task<IActionResult> Post( [FromBody] MyRequestDto dto )
    {
        // 
    }
}