.NET 4.8 ASP.NET Swagger Swagger swashbuckle请求包含一个实体主体,但没有内容类型的标头。媒体类型应用程序/不支持八位钟
您好,我在配置/测试 swashbuckle/Swagger 5.6.0使用.NET Framework 4.8 (asp.net [不是ASP.NET Core!])
测试从Webui Swagger抛出了一个错误。
与Postman进行相同的操作是:
-
content -type:“ application/json” - ok
-
content-type:“ application/ocet-stream” - 相同错误
响应主体错误:
Message: "The request contains an entity body but no Content-Type header. The inferred media type 'application/octet-stream' is not supported for this resource.",
ExceptionMessage: "No MediaTypeFormatter is available to read an object of type 'SomeParams' from content with media type 'application/octet-stream'.",
ExceptionType: "System.Net.Http.UnsupportedMediaTypeException",
StackTrace: " w System.Net.Http.HttpContentExtensions.ReadAsAsync[T](HttpContent content, Type type, IEnumerable`1 formatters, IFormatterLogger formatterLogger, CancellationToken cancellationToken)\r\n w System.Web.Http.ModelBinding.FormatterParameterBinding.ReadContentAsync(HttpRequestMessage request, Type type, IEnumerable`1 formatters, IFormatterLogger formatterLogger, CancellationToken cancellationToken)"
缺少请求内容类型标头的问题,我无法在SwaggerWebui内正确设置它。
没有在Asp.net swashbuckle中产生/消费属性,因此我按照说明在以下说明中手动添加它们(customAttribute + CustomFilters + CustomFilters +注册)。 但是它不起作用,参数内容类型combobox却没有出现。
我还尝试了编辑 webapiconfig.cs 带有/没有全局/正常格式的添加/清算支持的介质类型等。
//config.Formatters.JsonFormatter.SupportedMediaTypes.Add(new System.Net.Http.Headers.MediaTypeHeaderValue("application/json"));
var mediaType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/json");
var formatter = new System.Net.Http.Formatting.JsonMediaTypeFormatter();
formatter.SupportedMediaTypes.Clear();
formatter.SupportedMediaTypes.Add(mediaType);
config.Formatters.Clear();
config.Formatters.Add(formatter);
var jsonFormatter = new System.Net.Http.Formatting.JsonMediaTypeFormatter();
jsonFormatter.SupportedMediaTypes.Clear();
jsonFormatter.SupportedMediaTypes.Add(new MediaTypeHeaderValue("application/json"));
GlobalConfiguration.Configuration.Formatters.Add(jsonFormatter);
config.Formatters.JsonFormatter.SupportedMediaTypes.Add(new MediaTypeHeaderValue("application/json"));
config.Formatters.JsonFormatter.SupportedMediaTypes.Add(new MediaTypeHeaderValue("application/xml"));
样品控制器代码:
using System.Collections.Generic;
using System.Linq;
using System.Web.Http;
namespace MainClass
{
public class xxx_V1Controller : ApiController
{
[HttpGet]
[SwaggerConsumes("application/json")]
[SwaggerProduces("application/json")]
[Route("api/xxx_V1/GetAll/", Name = "xxx_V1_GetAll")]
public Rxxx_V1 GetAll(SomeParamsXyz)
{
return AuthorizationController.MainClass.yyy_GetAll_V1(SomeParams xyz);
}
[HttpGet]
[Route("api/xxx_V1/GetAll2/", Name = "xxx_V1_GetAll2")]
public Rxxx_V1 GetAll2(SomeParams xyz)
{
return AuthorizationController.MainClass.yyy_GetAll2_V1(SomeParams xyz);
}
}
}
SomeParams类
public class SomeParams : IXyz, IFilters
{
public string XXX { get; set; }
public bool GetCountOnly { get; set; }
public bool YYY{ get; set; }
public LoadOptions Options { get; set; }
public List<Filter> Filters { get; set; }
}
http:// localhost:20220/Swagger/Swagger/docs/v1 fragment:custom custom fragment:
"/api/xxx_V1/GetAll": {
"get": {
"tags": [
"xxx_V1"
],
"operationId": "xxx_V1_GetAll",
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"parameters": [
{
"name": "options.xxx",
"in": "query",
"required": false,
"type": "string"
},
{
"name": "options.getCountOnly",
"in": "query",
"required": false,
"type": "boolean"
},
{
"name": "options.YYY",
"in": "query",
"required": false,
"type": "boolean"
},
{
"name": "options.options.getCountOnly",
"in": "query",
"required": false,
"type": "boolean"
},
{
"name": "options.options.YYY",
"in": "query",
"required": false,
"type": "boolean"
},
{
"name": "options.filters",
"in": "query",
"required": false,
"type": "array",
"items": {},
"collectionFormat": "multi"
}
],
"responses": {
"200": {
"description": "OK",
"schema": {
"$ref": "#/definitions/xxxx.Objects.Rxxx_V1"
}
}
}
}
},
自 定义过滤器消耗 /生产精力工作的所有其他获取方法,而没有这些属性的json也具有应用程序 / XML。
"consumes": [
"application/json",
"application/xml"
],
"produces": [
"application/json",
"application/xml"
],
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我找到了一个网站,说Swashbuckler团队决定不支持尸体接听电话。我尝试了以上所有解决方案,但没有一个对我有用。对我来说,解决方案是将动作更改为帖子。抱歉,我没有讨论的链接。但是这是Github报告的错误,但已关闭。 https://github.com/swagger.com/swagger-appi-appi/swagger-appi/swagger/swagger-ui/issues/- 5388
I found a site that stated the Swashbuckler team decided to not support GET calls with a Body. I tried all the above solutions, but none worked for me. The solution for me was to change the action to a POST. Sorry I don't have a link to the discussion. But here is GitHub reporting it as Bug, but it was closed. https://github.com/swagger-api/swagger-ui/issues/5388