Asp.net web api Swashback和MediaTypeFormatter出现问题
我正在尝试在我的WebApi上应用Swashback。在大多数情况下,它是有效的 对于某些Api端点,对于Asp.net web api Swashback和MediaTypeFormatter出现问题,asp.net-web-api,swagger,swashbuckle,mediatypeformatter,Asp.net Web Api,Swagger,Swashbuckle,Mediatypeformatter,我正在尝试在我的WebApi上应用Swashback。在大多数情况下,它是有效的 对于某些Api端点,对于GET,方法接受来自查询字符串的JSON作为参数。为了将数据从JSON转换为.NET对象,这里有一些MediaTypeFormatter。MediaTypeFormatters通常如下所示: public class MyMediaTypeFormatter : JsonMediaTypeFormatter { public override bool CanReadType(Typ
GET
,方法接受来自查询字符串的JSON
作为参数。为了将数据从JSON
转换为.NET对象,这里有一些MediaTypeFormatter
。MediaTypeFormatter
s通常如下所示:
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
}
}
我需要为此指定任何显式配置吗?据我所见,我猜Swashback将该参数解释为主体参数而不是查询参数。你能分享一个
GET
方法吗?@venerik谢谢你的评论。我现在无法访问我的系统。因此,很难共享GET
请求。然而,方法参数是一个相对复杂的对象图,例如,一个包含不同类型对象数组的对象等等。
{
"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)"
}