No MediaTypeFormatter is available to read an object of type 'Advertisement' in asp.net web api

戏子无情 提交于 2019-12-05 10:20:05

问题


I have a class that name is Advertisement:

 public class Advertisement
{
    public string Title { get; set; }
    public string Desc { get; set; }
}

and in my 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());
    }

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)"

Can AnyBody help me?


回答1:


In your WebApiConfig.cs add this inside of register

config.Formatters.JsonFormatter.SupportedMediaTypes.Add(new MediaTypeHeaderValue("application/octet-stream"));



回答2:


"ExceptionMessage": "No MediaTypeFormatter is available to read an object of type 'Advertisement' from content with media type 'application/octet-stream'.",

That means that your application cannot read octet-stream Content-Type - which is what the request provided. This is one frustration I have with Web API. However there is a way around it. The easy way is to modify the Content-type to 'application/json' or 'application/xml' which is easily read. The harder way is to provide your own MediaTypeFormatter.




回答3:


Several issues:

  1. Instead of posting it as application/octet-stream, use application/json;charset=UTF-8, instead.
  2. public IHttpActionResult Test(Advertisement advertisement) needs to have [FromBody] in it:

    public IHttpActionResult Test([FromBody]Advertisement advertisement) { ... }

    By default, the ApiController expects anything being passed in to represent URL parameters, so you'd need that [FromBody] for any data you are posting in the Request Body that you want parsed out.

  3. You need to decorate your Post method with [System.Web.Http.HttpPost] so that it doesn't think it's the MVC version [System.Web.Mvc.HttpPost]. Ensure you put the full thing, because [HttpPost] will also default to the MVC version. It's probably not a bad idea to rename Test to Post, as well, though you might be using that for a Unit Test method, so not sure on that one.

  4. Send your data as JSON: { Title: "some title", Desc: "some description" }

  5. Do something with advertisement inside your Post() function:

    string title = advertisement.Title; string desc = advertisement.Desc;



来源:https://stackoverflow.com/questions/30544009/no-mediatypeformatter-is-available-to-read-an-object-of-type-advertisement-in

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!