.NET 4.8 ASP.NET Swagger Swagger swashbuckle请求包含一个实体主体,但没有内容类型的标头。媒体类型应用程序/不支持八位钟

发布于 2025-02-11 18:25:37 字数 5863 浏览 1 评论 0 原文

您好,我在配置/测试 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"
],

Hello I have problem with configuring/testing Swashbuckle/Swagger 5.6.0 with .NET Framework 4.8 (ASP.NET [not ASP.NET Core!])

Testing GET methods from WebUI Swagger throws an error.

enter image description here

Doing the same with postman is:

  • Content-Type: "application/json" - OK

  • Content-Type: "application/ocet-stream" - same error

Response body error:

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)"

Looks like there's problem with missing Request Content-Type header and I am unable to set it up properly inside SwaggerWebUI.

There's no Produces / Consumes attribute in ASP.NET Swashbuckle so I added them manually (customattribute + customfilters + registration in SwaggerConfig.cs) following instructions from:
https://blog.kloud.com.au/2017/08/04/swashbuckle-pro-tips-for-aspnet-web-api-part-1/
But its not working and Parameter content type combobox doesn't appear.

I also have tried editing WebApiConfig.cs with/without Global/normal formatters adding/clearing SupportedMediaTTypes etc.

    //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"));

Sample Controller code:

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 Class

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/docs/v1 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"
                }
            }
        }
    }
},

Custom filters Consumes / Produces propably work beacuse all other Get Methods without those attribute has json with application/xml too.

"consumes": [
  "application/json",
  "application/xml"
],
"produces": [
  "application/json",
  "application/xml"
],

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

我偏爱纯白色 2025-02-18 18:25:37

我找到了一个网站,说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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文