【问题标题】:Using optional complex type action parameter for a POST Web API为 POST Web API 使用可选的复杂类型操作参数
【发布时间】:2015-08-06 18:58:28
【问题描述】:

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

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

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

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

如果我不将 userCredentials 设为可选,则一切正常。 userCredential 定义如下:

public class UserCredentials
{
    public string password { get; set; }
    public string username { get; set; }
}

【问题讨论】:

标签: asp.net asp.net-web-api http-post restful-architecture


【解决方案1】:

尝试将 API 定义更改为:

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

由于 UserCredentials 是引用类型,如果客户端不提供值,它将被设置为 null

【讨论】:

    猜你喜欢
    • 2017-06-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-18
    • 1970-01-01
    相关资源
    最近更新 更多