2

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.

1 Answer 1

2

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)
{
...
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.