labyrinth
labyrinth

Reputation: 1164

Using optional complex type action parameter for a POST Web API

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; }
}

Upvotes: 10

Views: 4873

Answers (1)

sree
sree

Reputation: 2367

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

Upvotes: 5

Related Questions