How do I set “Parameter content type” using Swashbuckle?

最后都变了- 提交于 2020-03-01 01:37:12

问题


My swagger ui shows "Parameter content type" with various entries: "application/json-patch+json", "text/json", "application/json", and "application/*+json".

I only want "application/json".

There's a similar unsolved issue on the repo, which uses this visual (older ui, but same idea):

Is there some way to set this?

Swashbuckle.AspNetCore version 4.0.1
Swashbuckle.AspNetCore.Filters version 4.5.5


回答1:


Use the [Produces] and [Consumes] attributes. Swashbuckle (and others, like NSwag) will convert them into the appropriate Swagger documentation.

The [Consumes] attribute's constructor's first parameter is 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___

Like so:

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


来源:https://stackoverflow.com/questions/55978068/how-do-i-set-parameter-content-type-using-swashbuckle

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!