问题
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; }
}
回答1:
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
来源:https://stackoverflow.com/questions/30435902/using-optional-complex-type-action-parameter-for-a-post-web-api