10

I am trying to write an API with the following prototype:

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

But when I make a request to this API, with or without userCredentials, I get the following error with response code as 500

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

If I do not make the userCredentials as optional, everything works fine. The userCredential definition is as follows:

public class UserCredentials
{
    public string password { get; set; }
    public string username { get; set; }
}
Soheil Alizadeh
  • 2,936
  • 11
  • 29
  • 56
labyrinth
  • 1,104
  • 3
  • 11
  • 32
  • Looks like this has the answer: http://stackoverflow.com/questions/17236527/asp-net-web-api-formatter-parameter-binding-exception – Karen B May 26 '15 at 15:02

1 Answers1

5

Try changing the API definition to:

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

As UserCredentials is a reference type, if client doesn't provide a value it will be set to null

sree
  • 2,287
  • 28
  • 26