Using optional complex type action parameter for a POST Web API

 ̄綄美尐妖づ 提交于 2020-01-14 07:15:12

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!