为POST Web API使用可选的复杂类型操作参数 [英] Using optional complex type action parameter for a POST Web API
本文介绍了为POST Web API使用可选的复杂类型操作参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试使用以下原型编写API:
I am trying to write an API with the following prototype:
[HttpPost]
public ApplicationStatus appLogIn(string id, UserCredentials userCredentials = null)
但当我向此API发出请求时,无论是否有 userCredentials
,我收到以下错误,响应代码为 500
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
}
如果我不将userCredentials作为可选项,一切正常。 userCredential定义如下:
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; }
}
推荐答案
尝试更改API定义to:
Try changing the API definition to:
[HttpPost]
public ApplicationStatus appLogIn(string id, UserCredentials userCredentials)
由于UserCredentials是引用类型,如果客户端未提供值,则将其设置为null
As UserCredentials is a reference type, if client doesn't provide a value it will be set to null
这篇关于为POST Web API使用可选的复杂类型操作参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文