我有一个名为 Advertisement 的类:
public class Advertisement
{
public string Title { get; set; }
public string Desc { get; set; }
}
在我的 Controller 中:
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());
}
但是当我用 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)"
任何人都可以帮助我吗?
最佳答案
在您的 WebApiConfig.cs
中将其添加到寄存器中
config.Formatters.JsonFormatter.SupportedMediaTypes.Add(new MediaTypeHeaderValue("application/octet-stream"));
关于c# - 没有 MediaTypeFormatter 可用于读取 asp.net web api 中类型为 'Advertisement' 的对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30544009/