Meh*_*adi 18 c# asp.net asp.net-identity asp.net-web-api2
我有一个名为广告的类:
public class Advertisement
{
public string Title { get; set; }
public string Desc { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
在我的控制器中:
public class OrderController : ApiController
{
public UserManager<IdentityUser> UserManager { get; private set; }
// Post api/Order/Test
[Route("Test")]
public IHttpActionResult Test(Advertisement advertisement)
{
var currentUser = User.Identity.GetUserId();
Task<IdentityUser> user = UserManager.FindByIdAsync(currentUser);
return Ok(User.Identity.GetUserId());
}
Run Code Online (Sandbox Code Playgroud)
但是当我用Postman测试它时我遇到了这个错误,
"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 'Advertisement' 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)"
Run Code Online (Sandbox Code Playgroud)
有谁能够帮我?
Lou*_*eda 39
在你WebApiConfig.cs
添加这个寄存器里面
config.Formatters.JsonFormatter.SupportedMediaTypes.Add(new MediaTypeHeaderValue("application/octet-stream"));
Run Code Online (Sandbox Code Playgroud)
"ExceptionMessage": "没有 MediaTypeFormatter 可用于从媒体类型为“application/octet-stream”的内容中读取类型为“广告”的对象。",
这意味着您的应用程序无法读取八位字节流内容类型 - 这是请求提供的内容。这是我对 Web API 的一大不满。不过,有一种方法可以解决这个问题。简单的方法是将 Content-type 修改为易于阅读的“application/json”或“application/xml”。更困难的方法是提供您自己的 MediaTypeFormatter。