WebAPI Multiple Put/Post parameters

.Netasp.net Web-Api

.Net Problem Overview


I am trying to post multiple parameters on a WebAPI controller. One param is from the URL, and the other from the body. Here is the url: /offers/40D5E19D-0CD5-4FBD-92F8-43FDBB475333/prices/

Here is my controller code:

public HttpResponseMessage Put(Guid offerId, OfferPriceParameters offerPriceParameters)
{
    //What!?
    var ser = new DataContractJsonSerializer(typeof(OfferPriceParameters));
    HttpContext.Current.Request.InputStream.Position = 0;
    var what = ser.ReadObject(HttpContext.Current.Request.InputStream);

    return new HttpResponseMessage(HttpStatusCode.Created);
}

The content of the body is in JSON:

{
	"Associations":
    {
    	"list": [
        {
        	"FromEntityId":"276774bb-9bd9-4bbd-a7e7-6ed3d69f196f",
            "ToEntityId":"ed0d2616-f707-446b-9e40-b77b94fb7d2b",
            "Types":
            {
            	"list":[
                {
                	"BillingCommitment":5,
                    "BillingCycle":5,
                    "Prices":
                    {
                    	"list":[
                        {
                        	"CurrencyId":"274d24c9-7d0b-40ea-a936-e800d74ead53",
                            "RecurringFee":4,
                            "SetupFee":5
                        }]
                    }
             	}]
         	}
      	}]
 	}
}

Any idea why the default binding is not able to bind to the offerPriceParameters argument of my controller? It is always set to null. But I am able to recover the data from the body using the DataContractJsonSerializer.

I also try to use the FromBody attribute of the argument but it does not work either.

.Net Solutions


Solution 1 - .Net

[HttpPost]
public string MyMethod([FromBody]JObject data)
{
    Customer customer = data["customerData"].ToObject<Customer>();
    Product product = data["productData"].ToObject<Product>();
    Employee employee = data["employeeData"].ToObject<Employee>();
    //... other class....
}

using referance

using Newtonsoft.Json.Linq;

Use Request for JQuery Ajax

var customer = {
    "Name": "jhon",
    "Id": 1,
};
var product = {
    "Name": "table",
    "CategoryId": 5,
    "Count": 100
};
var employee = {
    "Name": "Fatih",
    "Id": 4,
};

var myData = {};
myData.customerData = customer;
myData.productData = product;
myData.employeeData = employee;

$.ajax({
    type: 'POST',
    async: true,
    dataType: "json",
    url: "Your Url",
    data: myData,
    success: function (data) {
        console.log("Response Data ↓");
        console.log(data);
    },
    error: function (err) {
        console.log(err);
    }
});

Solution 2 - .Net

Natively WebAPI doesn't support binding of multiple POST parameters. As Colin points out there are a number of limitations that are outlined in my blog post he references.

There's a workaround by creating a custom parameter binder. The code to do this is ugly and convoluted, but I've posted code along with a detailed explanation on my blog, ready to be plugged into a project here:

Passing multiple simple POST Values to ASP.NET Web API

Solution 3 - .Net

If attribute routing is being used, you can use the [FromUri] and [FromBody] attributes.

Example:

[HttpPost()]
[Route("api/products/{id:int}")]
public HttpResponseMessage AddProduct([FromUri()] int id,  [FromBody()] Product product)
{
  // Add product
}

Solution 4 - .Net

We passed Json object by HttpPost method, and parse it in dynamic object. it works fine. this is sample code:

webapi:

[HttpPost]
public string DoJson2(dynamic data)
{
   //whole:
   var c = JsonConvert.DeserializeObject<YourObjectTypeHere>(data.ToString()); 
   
   //or 
   var c1 = JsonConvert.DeserializeObject< ComplexObject1 >(data.c1.ToString());

   var c2 = JsonConvert.DeserializeObject< ComplexObject2 >(data.c2.ToString());

   string appName = data.AppName;
   int appInstanceID = data.AppInstanceID;
   string processGUID = data.ProcessGUID;
   int userID = data.UserID;
   string userName = data.UserName;
   var performer = JsonConvert.DeserializeObject< NextActivityPerformers >(data.NextActivityPerformers.ToString());

   ...
}

The complex object type could be object, array and dictionary.

ajaxPost:
...
Content-Type: application/json,
data: {"AppName":"SamplePrice",
       "AppInstanceID":"100",
       "ProcessGUID":"072af8c3-482a-4b1c‌​-890b-685ce2fcc75d",
       "UserID":"20",
       "UserName":"Jack",
       "NextActivityPerformers":{
           "39‌​c71004-d822-4c15-9ff2-94ca1068d745":[{
                 "UserID":10,
                 "UserName":"Smith"
           }]
       }}
...

Solution 5 - .Net

A simple parameter class can be used to pass multiple parameters in a post:

public class AddCustomerArgs
{
    public string First { get; set; }
    public string Last { get; set; }
}

[HttpPost]
public IHttpActionResult AddCustomer(AddCustomerArgs args)
{
    //use args...
    return Ok();
}

Solution 6 - .Net

Nice question and comments - learnt much from the replies here :)

As an additional example, note that you can also mix body and routes e.g.

[RoutePrefix("api/test")]
public class MyProtectedController 
{
    [Authorize]
    [Route("id/{id}")]
    public IEnumerable<object> Post(String id, [FromBody] JObject data)
    {
        /*
          id                                      = "123"
          data.GetValue("username").ToString()    = "user1"
          data.GetValue("password").ToString()    = "pass1"
         */
    }
}

Calling like this:

POST /api/test/id/123 HTTP/1.1
Host: localhost
Accept: application/json
Content-Type: application/x-www-form-urlencoded
Authorization: Bearer x.y.z
Cache-Control: no-cache

username=user1&password=pass1


enter code here

Solution 7 - .Net

You can allow multiple POST parameters by using the MultiPostParameterBinding class from https://github.com/keith5000/MultiPostParameterBinding

To use it:

  1. Download the code in the Source folder and add it to your Web API project or any other project in the solution.

  2. Use attribute [MultiPostParameters] on the action methods that need to support multiple POST parameters.

    [MultiPostParameters] public string DoSomething(CustomType param1, CustomType param2, string param3) { ... }

  3. Add this line in Global.asax.cs to the Application_Start method anywhere before the call to GlobalConfiguration.Configure(WebApiConfig.Register):

    GlobalConfiguration.Configuration.ParameterBindingRules.Insert(0, MultiPostParameterBinding.CreateBindingForMarkedParameters);

  4. Have your clients pass the parameters as properties of an object. An example JSON object for the DoSomething(param1, param2, param3) method is:

    { param1:{ Text:"" }, param2:{ Text:"" }, param3:"" }

Example JQuery:

$.ajax({
	data: JSON.stringify({ param1:{ Text:"" }, param2:{ Text:"" }, param3:"" }),
	url: '/MyService/DoSomething',
	contentType: "application/json", method: "POST", processData: false
})
.success(function (result) { ... });

Visit the link for more details.

Disclaimer: I am directly associated with the linked resource.

Solution 8 - .Net

2021 and there are new solutions. Pradip Rupareliya suggested a good one, that I'll complement using only Dict, instead of a helper data structure as He did:

[HttpPost]
public ActionResult MakePurchase([FromBody] Dictionary<string, string> data)
{
    try
    {
        int userId = int.Parse(data["userId"]);
        float boughtAmountInARS = float.Parse(data["boughtAmountInARS"]);
        string currencyName = data["currencyName"];
    }
    catch (KeyNotFoundException)
    {
        return BadRequest();
    }
    catch (FormatException)
    {
        return BadRequest();
    }
}

Solution 9 - .Net

What does your routeTemplate look like for this case?

You posted this url:

/offers/40D5E19D-0CD5-4FBD-92F8-43FDBB475333/prices/

In order for this to work I would expect a routing like this in your WebApiConfig:

routeTemplate: {controller}/{offerId}/prices/

Other assumptions are:

  • your controller is called OffersController.
  • the JSON object you are passing in the request body is of type OfferPriceParameters (not any derived type)
  • you don't have any other methods on the controller that could interfere with this one (if you do, try commenting them out and see what happens)

And as Filip mentioned it would help your questions if you started accepting some answers as 'accept rate of 0%' might make people think that they are wasting their time

Solution 10 - .Net

If you don't want to go ModelBinding way, you can use DTOs to do this for you. For example, create a POST action in DataLayer which accepts a complex type and send data from the BusinessLayer. You can do it in case of UI->API call.

Here are sample DTO. Assign a Teacher to a Student and Assign multiple papers/subject to the Student.

public class StudentCurriculumDTO
 {
     public StudentTeacherMapping StudentTeacherMapping { get; set; }
     public List<Paper> Paper { get; set; }
 }    
public class StudentTeacherMapping
 {
     public Guid StudentID { get; set; }
     public Guid TeacherId { get; set; }
 }

public class Paper
 {
     public Guid PaperID { get; set; }
     public string Status { get; set; }
 }

Then the action in the DataLayer can be created as:

[HttpPost]
[ActionName("MyActionName")]
public async Task<IHttpActionResult> InternalName(StudentCurriculumDTO studentData)
  {
     //Do whatever.... insert the data if nothing else!
  }

To call it from the BusinessLayer:

using (HttpResponseMessage response = await client.PostAsJsonAsync("myendpoint_MyActionName", dataof_StudentCurriculumDTO)
  {
     //Do whatever.... get response if nothing else!
  }

Now this will still work if I wan to send data of multiple Student at once. Modify the MyAction like below. No need to write [FromBody], WebAPI2 takes the complex type [FromBody] by default.

public async Task<IHttpActionResult> InternalName(List<StudentCurriculumDTO> studentData)

and then while calling it, pass a List<StudentCurriculumDTO> of data.

using (HttpResponseMessage response = await client.PostAsJsonAsync("myendpoint_MyActionName", List<dataof_StudentCurriculumDTO>)

Solution 11 - .Net

Request parameters like

enter image description here

Web api Code be like

public class OrderItemDetailsViewModel
{
    public Order order { get; set; }
    public ItemDetails[] itemDetails { get; set; }
}

public IHttpActionResult Post(OrderItemDetailsViewModel orderInfo)
{
    Order ord = orderInfo.order;
    var ordDetails = orderInfo.itemDetails;
    return Ok();
}

Solution 12 - .Net

You can get the formdata as string:

    protected NameValueCollection GetFormData()
    {
        string root = HttpContext.Current.Server.MapPath("~/App_Data");
        var provider = new MultipartFormDataStreamProvider(root);

        Request.Content.ReadAsMultipartAsync(provider);

        return provider.FormData;
    }

    [HttpPost]
    public void test() 
    {
        var formData = GetFormData();
        var userId = formData["userId"];

        // todo json stuff
    }

https://docs.microsoft.com/en-us/aspnet/web-api/overview/advanced/sending-html-form-data-part-2

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionNormand BedardView Question on Stackoverflow
Solution 1 - .NetFatih GÜRDALView Answer on Stackoverflow
Solution 2 - .NetRick StrahlView Answer on Stackoverflow
Solution 3 - .NetBryan RaynerView Answer on Stackoverflow
Solution 4 - .NetBes LeyView Answer on Stackoverflow
Solution 5 - .NetGreg GumView Answer on Stackoverflow
Solution 6 - .NetAnthony De SouzaView Answer on Stackoverflow
Solution 7 - .NetKeithView Answer on Stackoverflow
Solution 8 - .NetAlvaro Rodriguez ScelzaView Answer on Stackoverflow
Solution 9 - .NetJoanna DerksView Answer on Stackoverflow
Solution 10 - .NetsandiejatView Answer on Stackoverflow
Solution 11 - .NetPradip RupareliyaView Answer on Stackoverflow
Solution 12 - .NetMartien de JongView Answer on Stackoverflow