webbuilder
webbuilder

Reputation: 173

JSON string passing to POST in web api - cant bind Multiple parameters error

I am passing two complex parameters to Web Api Post method and getting error "cant bind multiple parameters".I am using fiddler to test it and sending JSON string with composer.

Here is my JSON string.

{"agentEntity":[{"AgentID":"1","ClientID":"1"}],"userEntity":[{"ClientID":"1","UserID":"1"}]}

I have validated this string on JSONLint.com and it says OK.

When i write POST method with one parameter and pass below string then it works OK.

{"agentEntity":[{"AgentID":"1","ClientID":"1"}]}

My Web api Post method.

public OperationStatus Post(AgentEntity agentEntity, UserEntity userEntity)
    {...
}

please help.

Upvotes: 2

Views: 2035

Answers (1)

Cybermaxs
Cybermaxs

Reputation: 24558

Web API does not deal with multiple posted content values, you can only post a single content value to a Web API Action method. You can read here how parameter binding works.

However, there are a few workarounds :

  • Use a single parameter

Create a model having all parameters to be passed, named for example PostRequest

public OperationStatus Post(PostRequest request)
{
   ...
}
  • Use JObject

As asp.net Web Api now uses JSON.NET for it’s JSON serializer, So you can use the JObject class to receive & parse a dynamic JSON result

public OperationStatus Post(JObject data) 
{
 ...
}
  • Use FormDataCollection

You can define FormDataCollection type argument and read parameter one by one manually using .Get() or .GetValues() methods (for multi-select values).

public OperationStatus Post(FormDataCollection data)
{
...
}

Upvotes: 2

Related Questions