如何设置“参数内容类型"使用Swashbuckle吗? [英] How do I set "Parameter content type" using Swashbuckle?

查看:193
本文介绍了如何设置“参数内容类型"使用Swashbuckle吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的招摇用户界面显示参数内容类型",其中包含各种条目:"application/json-patch+json""text/json""application/json""application/*+json".

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

我只想要"application/json".

回购协议上有一个相似未解决的问题,该问题使用了这种视觉效果(旧版ui ,但想法相同):

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

有什么设置方法吗?

Swashbuckle.AspNetCore版本4.0.1
Swashbuckle.AspNetCore.Filters版本4.5.5

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

推荐答案

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

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

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

The [Consumes] attribute's constructor's first parameter is String contentType:

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

像这样:

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

这篇关于如何设置“参数内容类型"使用Swashbuckle吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆