没有MediaTypeFormatter可用于读取ASP.NET Web API中类型为“广告"的对象 [英] No MediaTypeFormatter is available to read an object of type 'Advertisement' in asp.net web api
本文介绍了没有MediaTypeFormatter可用于读取ASP.NET Web API中类型为“广告"的对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一堂课,名为广告":
I have a class that name is Advertisement:
public class Advertisement
{
public string Title { get; set; }
public string Desc { get; set; }
}
并在我的控制器中:
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测试它时,我会遇到此错误,
but when I test it with Postman I face this error,
"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)"
AnyBody可以帮助我吗?
Can AnyBody help me?
推荐答案
在您的WebApiConfig.cs
中将其添加到寄存器中
In your WebApiConfig.cs
add this inside of register
config.Formatters.JsonFormatter.SupportedMediaTypes.Add(new MediaTypeHeaderValue("application/octet-stream"));
这篇关于没有MediaTypeFormatter可用于读取ASP.NET Web API中类型为“广告"的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文