2015-05-30 2 views
11

ich eine Klasse, die Namen Werbung ist:Kein MediaTypeFormatter ist verfügbar ein Objekt vom Typ 'Werbung' in asp.net web api zu lesen

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

und in meinem 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()); 
    } 

aber wenn ich es mit Postman teste ich diesen Fehler konfrontiert,

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

Kann jemand mir helfen?

+0

Aus dem Fehler i erscheint, dass Sie keine Content-Type-Header haben? Fügen Sie den entsprechenden Header Content-Type in Ihren Postman-Aufruf ein, der das Problem beheben sollte. –

+0

Ich habe "Content-Type" und "Accept" in meinen Kopfzeilen @DaveAgaba –

+0

Überprüfen Sie meine Antwort unten. –

Antwort

2

"ExceptionMessage": "Keine MediaTypeFormatter verfügbar sind ein Objekt vom Typ 'Werbung' von Inhalten mit Medientyp 'application/octet-stream' zu lesen",

Das bedeutet, dass Ihre Anwendung kann den Content-Typ von octet-stream nicht lesen - was die Anforderung darstellt. Dies ist eine Frustration, die ich mit Web API habe. Es gibt jedoch einen Weg um es herum. Der einfache Weg besteht darin, den Inhaltstyp in 'application/json' oder 'application/xml' zu ändern, was leicht zu lesen ist. Der schwierigere Weg besteht darin, einen eigenen MediaTypeFormatter bereitzustellen.

+0

Der 'dieser' Tutorial-Link ist jetzt tot. – NStuke

25

In Ihrem WebApiConfig.cs diese innerhalb des Registers hinzufügen

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

Dies funktioniert nicht. config.Formatters.JsonFormatter.CanWriteType (typeof (MyModel)); gibt false zurück. – JDC

Verwandte Themen