我正在C#项目中运行单元测试,并且第一次看到此错误:
System.Net.Http.UnsupportedMediaTypeException : No MediaTypeFormatter is available to read an object of type 'MyCustomModel' from content with media type 'text/plain'.
引起问题的行如下:
using (HttpResponseMessage response = await client.RawGetAsync(cancellationToken, string.Format("{0}/items", (object) service, (object) id), new KeyValuePair<string, string>[0]).ConfigureAwait(false))
listModel= await client.ReadContentAsync<ListModel>(response, token).ConfigureAwait(false);
在阅读内容时,是否可以指定SupportedMediaType
?
类似的东西:
listModel = response.Content.ReadAsAsync<ListModel>(
new List<MediaTypeFormatter>{
new XmlMediaTypeFormatter(),
new JsonMediaTypeFormatter()}).Result;
我可以在通话中使用SupportedMediaTypes.Add(new MediaTypeHeaderValue("text/plain"));
吗?
谢谢