2015-05-25 13 views
9

Sto cercando di scrivere un'API con il seguente prototipo:Usando il parametro di tipo complesso un'azione facoltativa per un API POST Web

[HttpPost] 
public ApplicationStatus appLogIn(string id, UserCredentials userCredentials = null) 

Ma quando faccio una richiesta a questa API, con o senza userCredentials, ottengo il seguente errore con codice di risposta come 500

{ 
    "Message": "An error has occurred.", 
    "ExceptionMessage": "Optional parameter 'userCredentials' is not supported by 'FormatterParameterBinding'.", 
    "ExceptionType": "System.InvalidOperationException", 
    "StackTrace": null 
} 

Se non fanno i UserCredentials come optional, tutto funziona bene. La definizione userCredential è il seguente:

public class UserCredentials 
{ 
    public string password { get; set; } 
    public string username { get; set; } 
} 
+0

Sembra che questo abbia la risposta: http://stackoverflow.com/questions/17236527/asp-net-web-api-formatter-parameter-binding-exception –

risposta

4

provare a cambiare la definizione API per:

[HttpPost] 
public ApplicationStatus appLogIn(string id, UserCredentials userCredentials) 

Come UserCredentials è un tipo di riferimento, se il client non fornisce un valore sarà impostato su null

Problemi correlati