运行核心Web 2.0的API。本地当我通过邮差张贴到我控制器,一切工作正常。不过,我得到:
没有MediaTypeFormatter是可用于从与媒体类型文本/ html“内容读类型“消息”的目的
当部署到生产服务器
控制器:
[HttpPost]
public void Post([FromQuery] Message message)
{
_service.UserRequest(message);
}
信息:
public class Message : IEntity
{
public string To { get; set; } = string.Empty;
public string From { get; set; } = string.Empty;
public string Body { get; set; } = string.Empty;
}
不知道是什么导致了这一点。它是MIME类型在IIS中配置?
我尝试了以前的答案与收紧代码的目标的这个变体:
var obj = await response.Content.ReadAsAsync<T>( new [] { new JsonMediaTypeFormatter() });
但是,它仍然抱怨,因为我的反应仍然没有正确的内容标题。
所以,我想这改变了头,这对我的作品,但我真的不知道这有我只是还没有通过尚未想到的任何意外的副作用:
if(response.Content?.Headers?.ContentType != null)
{
response.Content.Headers.ContentType.MediaType = "application/json";
}
这个固定(增加了新的格式化程序):
var obj = await response.Content.ReadAsAsync<T>(
new List<MediaTypeFormatter>
{
new XmlMediaTypeFormatter(),
new JsonMediaTypeFormatter()
});