【发布时间】:2018-02-09 13:21:48
【问题描述】:
Web API 代码:
[HttpPost]
[ODataRoute("GetCMMAutoForLoggedInUser")]
public IHttpActionResult GetCMMAutoForLoggedInUser(CMMPermissionRequest request)
{
var data = this.CommonDomain.GetCMMAutoForLoggedInUser(request);
return Ok(data);
}
在正文中我在 JSON 下方指定:
{ "EnterPriseId": "prasad.kiran.shigwan",
"LocationLevelId": "5",
"LocationLevelValue": "SZ"
}
但在 POSTMAN 工具中出现异常:
{
"error": {
"code": "",
"message": "The request entity's media type 'application/json' is not supported for this resource.",
"innererror": {
"message": "No MediaTypeFormatter is available to read an object of type 'CMMPermissionRequest' from content with media type 'application/json'.",
"type": "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.Web.Http.ModelBinding.FormatterParameterBinding.ReadContentAsync(HttpRequestMessage request, Type type, IEnumerable`1 formatters, IFormatterLogger formatterLogger, CancellationToken cancellationToken)"
}
}
}
请参阅下面来自 POSTMAN 的 json 屏幕截图:
【问题讨论】:
-
另一端的服务器是否支持 json - 或者是 xml 还是什么?
-
请记住,当您从下拉列表中选择
JSON(application/json)时,POSTMAN 会自动为您创建一个标题(Content-Type:application/json) -
@Andez 是的,它支持 json。
-
有几件事:1) Content-Type 实际上只适用于 POST,因为它描述了您向 API 发送 的内容; 2) 对于 GET 请求,您应该将 Accept 标头设置为 application/json 以告诉 API 您希望返回 JSON。 ASP.NET WebApi 支持 JSON,除非您在
WebApiConfig.Register方法中更改了config.Formatters集合的内容。
标签: angular asp.net-web-api asp.net-web-api2