为POST Web API使用可选的复杂类型操作参数

lab*_*nth 10 asp.net http-post restful-architecture asp.net-web-api

我正在尝试使用以下原型编写API:

[HttpPost]
public ApplicationStatus appLogIn(string id, UserCredentials userCredentials = null)
Run Code Online (Sandbox Code Playgroud)

但是当我向这个API提出请求时,无论有没有userCredentials,我都会得到以下错误,响应代码为500

{
  "Message": "An error has occurred.",
  "ExceptionMessage": "Optional parameter 'userCredentials' is not supported by 'FormatterParameterBinding'.",
  "ExceptionType": "System.InvalidOperationException",
  "StackTrace": null
}
Run Code Online (Sandbox Code Playgroud)

如果我不将userCredentials作为可选项,一切正常.userCredential定义如下:

public class UserCredentials
{
    public string password { get; set; }
    public string username { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

sre*_*ree 5

尝试将API定义更改为:

[HttpPost]
public ApplicationStatus appLogIn(string id, UserCredentials userCredentials)
Run Code Online (Sandbox Code Playgroud)

由于UserCredentials是引用类型,如果客户端未提供值,则将其设置为null