50

The below code are simplified to show the necessity. May I know what is wrong? I can't seems to retrieve two Parameters (A and B in this case) using the [FromBody] attribute.

The error message is "Can't bind multiple parameters ('A' and 'B') to the request's content"

It is perfectly fine if I have either A or B only.

Web API:

[Route("API/Test"), HttpPost] public IHttpActionResult Test([FromBody] int A, [FromBody] int B) 

Client:

HttpClient client = new HttpClient(); var content = new FormUrlEncodedContent( new Dictionary<string, string> { { "A", "123" }, { "B", "456" } }); client.PostAsync("http://localhost/API/Test", content).Result; 

4 Answers 4

69

Web Api doesn't support multiple [FromBody] params I think. But you can use Api model, to passing more parameters to your api action.:

public class YourApiModel { public int A{ get; set; } public int B { get; set; } //...other properties } 

After that, you can simply use this in your API controller Test:

 // POST: api/test public IHttpActionResult Post([FromBody] YourApiModel model) { //do something } 

Hope it help.

Sign up to request clarification or add additional context in comments.

1 Comment

Just to confirm what you've said, the microsoft documentation says "At most one parameter is allowed to read from the message body"
12

Try the Web API code:

[DataContract] public class Model { [DataMember] public int A { get; set; } [DataMember] public int B { get; set; } } [Route("API/Test"), HttpPost] public IHttpActionResult Test([FromUri] Model model) 

Comments

0

experienced a similar error while developing a c# REST Web API for Active Directory Authentication, see my error message below.

{ "Message": "An error has occurred.", "ExceptionMessage": "Can't bind multiple parameters ('userName' and 'passWord') to the request's content.", "ExceptionType": "System.InvalidOperationException", "StackTrace": " at System.Web.Http.Controllers.HttpActionBinding.ExecuteBindingAsync(HttpActionContext actionContext, CancellationToken cancellationToken)\r\n at System.Web.Http.Controllers.ActionFilterResult.d__5.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at System.Web.Http.Dispatcher.HttpControllerDispatcher.d__15.MoveNext()" }

I was able to resolve it using the below code

// code with issue [HttpPost] // POST: api/Showcredential public string Post([FromBody]string userName, [FromBody]string passWord) { return "Username: " + userName +" " + "Password: " + passWord; } // code used to resolve the issue using System; using System.Collections.Generic; using System.Linq; using System.Net; using System.Net.Http; using System.Web.Http; using Newtonsoft.Json; namespace ActiveDirectoryRESTApi.Controllers { public class ShowCredentialController : ApiController { public class Loginparam { public object username { get; set; } public object password { get; set; } } [HttpPost] public object Post([FromBody()] object Loginvalues) { var sessionresponse = new Loginparam(); string rawrequest = Loginvalues.ToString(); var jsonResulttodict = JsonConvert.DeserializeObject<Dictionary<string, object>>(rawrequest); string rsusername = jsonResulttodict["username"].ToString(); string rspassword = jsonResulttodict["password"].ToString(); return "Username: " + rsusername + " " + "Password: " + rspassword; } } } 

Comments

0

i wrote a generic class to manage all data type as list, to send everything. then on your body i can, for example:

int=1,int=3,int=5,string=fdsg,bool=0,ecc...

[HttpPost] public object Post([FromBody] Generic values) { return values.String; } public class Generic { public List<Nullable<byte>> Byte { get; set; } public List<Nullable<sbyte>> Sbyte { get; set; } public List<Nullable<short>> Short { get; set; } public List<Nullable<ushort>> Ushort { get; set; } public List<Nullable<int>> Int { get; set; } public List<Nullable<uint>> Uint { get; set; } public List<Nullable<long>> Long { get; set; } public List<Nullable<ulong>> Ulong { get; set; } public List<Nullable<float>> Float { get; set; } public List<Nullable<double>> Double { get; set; } public List<Nullable<decimal>> Decimal { get; set; } public List<Nullable<char>> Char { get; set; } //public List<object> Object { get; set; } public List<string> String { get; set; } public List<Nullable<DateTime>> Datetime { get; set; } //public List<bool> Bool { get; set; } //to mange also 0/1 public List<string> Bool { get; set; } private List<Nullable<bool>> Boolean { get { List<Nullable<bool>> tmp_b = new List<Nullable<bool>>(); foreach (string tmp in Bool) { switch (tmp.ToUpper()) { case "TRUE": case "1": tmp_b.Add(true); break; case "FALSE": case "0": tmp_b.Add(false); break; default: tmp_b.Add(null); break; } } return tmp_b; } } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.