2015-05-25 15 views
9

próbuję napisać API z następującym prototypie:Stosując opcjonalny parametr kompleks działań typu dla API POST Web

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

Ale kiedy złożyć wniosek do tego API, z lub bez userCredentials, mam następujący błąd z kodem odpowiedzi jako 500

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

Jeśli nie dokona userCredentials jako opcjonalne, wszystko działa bez zarzutu. UserCredential definicja jest następująca:

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

Wygląda na to, że ma odpowiedź: http://stackoverflow.com/questions/17236527/asp-net-web-api-formatter-parameter-binding-exception –

Odpowiedz

4

Spróbuj zmienić definicję API do:

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

Jak UserCredentials jest typ referencyjny, jeśli klient nie podać wartość zostanie ustawiona na null

Powiązane problemy