Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/xamarin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
.net 导入OpenAPI 2.0 Postman集合,所有POST请求正文显示为x-www-form-urlencoded,而不是原始JSON 我在用什么?_.net_Asp.net Web Api_Swagger_Postman_Swashbuckle - Fatal编程技术网

.net 导入OpenAPI 2.0 Postman集合,所有POST请求正文显示为x-www-form-urlencoded,而不是原始JSON 我在用什么?

.net 导入OpenAPI 2.0 Postman集合,所有POST请求正文显示为x-www-form-urlencoded,而不是原始JSON 我在用什么?,.net,asp.net-web-api,swagger,postman,swashbuckle,.net,Asp.net Web Api,Swagger,Postman,Swashbuckle,.NET Framework 4.8 邮递员版本7.19.1 Swashback 5.6.0,nugget套装 问题 我有一个使用Swagger(Swashback)的大API,我想将开放API模式作为一个集合导入Postman。 在执行{myUrl}/swagger/docs/v1并将模式粘贴到Postman中之后,我注意到我所有的Post请求都被“破坏”。当转到Body时,它显示为“application/x-www-form-urlencoded”,而不是原始的JSONBody 相反,

.NET Framework 4.8
邮递员版本7.19.1
Swashback 5.6.0,nugget套装


问题 我有一个使用Swagger(Swashback)的大API,我想将开放API模式作为一个集合导入Postman。
在执行
{myUrl}/swagger/docs/v1
并将模式粘贴到Postman中之后,我注意到我所有的Post请求都被“破坏”。当转到
Body
时,它显示为
“application/x-www-form-urlencoded”
,而不是原始的
JSON
Body

相反,它看起来是这样的:

我的模式:

{
    "swagger": "2.0",
    "info": {
        "version": "v1",
        "title": "My API"
    },
    "host": "localhost:51209",
    "schemes": [
        "http"
    ],
    "paths": { },
    "definitions": { }
}
路径
定义
不是空的,它们包含很多API调用,这就是为什么我只从它们中选择一个POST方法,因为它们都有相同的问题

下面是
路径
对象的一个示例,其中的属性
“consumes”

"/api/MyController/MyMethod": {
        "consumes": [
            "application/json",
            "text/json",
            "application/xml",
            "text/xml",
            "application/x-www-form-urlencoded"
        ]
}
问题似乎出在
使用的
对象属性中,该属性有一个字符串数组作为值,其中一个是
“application/x-www-form-urlencoded”

在.NET Web API中,我的操作或控制器上方没有“特殊”属性

[HttpPost]
[路线(“我的方法”)]
公共IHttpActionResult MyMethod(列出作业头列表)
我知道ASP Net Core有一个
Consumes
属性,但我使用的是.Net Framework。

解决方案: 我看到了这一点,并在那里实施了解决方案:

已创建自定义属性:

[AttributeUsage(AttributeTargets.Method | AttributeTargets.Class)]
公共类SwaggerConsumesAttribute:属性
{
public-SwaggerConsumesAttribute(参数字符串[]内容类型)
{
this.ContentTypes=ContentTypes;
}
公共IEnumerable内容类型{get;}
}
之后,为Swagger创建一个
OperationFilter
类:

公共类ConsumeSperationFilter:IOperationFilter
{
public void Apply(操作,SchemaRegistry SchemaRegistry,apisdescription apisdescription)
{
var attribute=apiscription.GetControllerAndActionAttributes().SingleOrDefault();
if(属性==null)
{
返回;
}
操作.consumes.Clear();
operation.consumes=attribute.ContentTypes.ToList();
}
}
最后,只需在
SwaggerConfig.cs
中注册
OperationFilter
,如下所示:

c.操作过滤器();

您可能需要添加或更改注释,以便生成的
使用的
不包括
应用程序/x-www-form-urlencoded
。无论如何,此媒体类型不与
in:body
参数一起使用。是的,这正是我所想的,据我所知,有一些属性可用于swagger,但找不到任何对此负责的属性。这正是我的问题,我不确定我必须把什么属性放进去。这回答了你的问题吗?它适用于ASP.NETCore2.2,找不到适用于.NETFramework4.8的正确版本。但事实上,这正是我要寻找的。