Swashbuckle和MediaTypeFormatter的问题

时间:2016-06-23 12:33:22

标签: asp.net-web-api swagger swashbuckle mediatypeformatter

我正在尝试在我的WebApi上应用Swashbuckle。在大多数情况下它都有效。

对于某些Api端点,对于GET,方法接受来自查询字符串的JSON作为参数。为了将数据从JSON转换为.NET对象,有一些MediaTypeFormatterMediaTypeFormatter通常如下所示:

public class MyMediaTypeFormatter : JsonMediaTypeFormatter
{
    public override bool CanReadType(Type type)
    {
        return type == typeof (MyType);
    }

    public override bool CanWriteType(Type type)
    {
        return false;
    }

    public override Task<object> ReadFromStreamAsync(Type type, Stream readStream, HttpContent content, IFormatterLogger formatterLogger,
        CancellationToken cancellationToken)
    {
        //Do deserialization here
    }
}

从我的WebApp和Postman调用时,我的WebApi可以正常工作。但是,当我从Swagger-UI调用相同的服务时,它会中断并抱怨:

{
  "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 'MyType' from content with media type 'application/octet-stream'.",
  "ExceptionType": "System.Net.Http.UnsupportedMediaTypeException",
  "StackTrace": "   at System.Net.Http.HttpContentExtensions.ReadAsAsync[T](HttpContent content, Type type, IEnumerable`1 formatters, IFormatterLogger formatterLogger, CancellationToken cancellationToken)\r\n   at System.Net.Http.HttpContentExtensions.ReadAsAsync(HttpContent content, Type type, IEnumerable`1 formatters, IFormatterLogger formatterLogger, CancellationToken cancellationToken)\r\n   at System.Web.Http.ModelBinding.FormatterParameterBinding.ReadContentAsync(HttpRequestMessage request, Type type, IEnumerable`1 formatters, IFormatterLogger formatterLogger, CancellationToken cancellationToken)"
}

我是否需要为此指定任何显式配置?

0 个答案:

没有答案