2015-05-30 8 views
11

ho una classe che nome è Pubblicità:Non MediaTypeFormatter è a disposizione per leggere un oggetto di tipo 'Pubblicita' in asp.net web api

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

e nel mio 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()); 
    } 

ma quando prova con postino mi faccia questo errore,

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

Qualcuno può aiutarmi?

+0

Da L'errore che sembra che non hai header Content-Type? Aggiungi l'intestazione appropriata Content-Type nella tua chiamata Postman e questo dovrebbe risolverlo. –

+0

Ho 'Content-Type' e' Accept' nelle mie intestazioni @DaveAgaba –

+0

Controlla la mia risposta qui sotto. –

risposta

2

"ExceptionMessage": "Non MediaTypeFormatter è a disposizione per leggere un oggetto di tipo 'Pubblicità' dai contenuti di tipo multimediale 'application/octet-stream'",

Ciò significa che l'applicazione impossibile leggere octet-stream Content-Type - che è ciò che la richiesta ha fornito. Questa è una frustrazione che ho con Web API. Tuttavia c'è un modo per aggirarlo. Il modo più semplice è modificare il tipo di contenuto in "application/json" o "application/xml" che è facilmente leggibile. Il modo più difficile è fornire il proprio MediaTypeFormatter.

+0

Il link del tutorial "questo" è morto. – NStuke

25

Nel vostro WebApiConfig.cs aggiungere questo all'interno del registro

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

Questo non funziona. config.Formatters.JsonFormatter.CanWriteType (typeof (MyModel)); restituisce falso. – JDC

Problemi correlati