Last post Feb 20, 2016 05:05 PM by Yohann Lu
Feb 12, 2016 09:24 AM|Stewa11|LINK
I have an API2 controller action I would like to pass several records to.
Here is the API2 Controller I would like to call. I want to call the API2 controller once and avoid several calls updating record-by-record.
// loop through the list and update remote database here.
I would like to know please what changes should I make to webapi2 controller action and to MVC controller action?
Should I pass in a serialized object? For example,
serialisedMeasureList = JsonConvert.SerializeObject(RecsForTR_ID,
Many thanks for viewing and posting answers.
Feb 12, 2016 03:36 PM|rtang2011|LINK
You may want to take a look at OData Batch. It even allows you to put different Web API calls in one batch. Since you are updating database, you may want to implement transaction also:
Feb 15, 2016 05:37 AM|Yohann Lu|LINK
You want to call the API2 controller once and avoid several calls updating record-by-record. I give you a suggestion in the below.
Firstly, you can design the API2 controller method and input parameters with “MEASURE_CAPTURE1” list (may not be serialized).
Secondly, in your API2 controller method, you can loop through “MEASURE_CAPTURE1” list and execute multiple SQL statement recorded by the SqlTransaction.
Information about sqlTransaction:
Feb 15, 2016 09:06 AM|Stewa11|LINK
Feb 15, 2016 09:08 AM|Stewa11|LINK
many thanks for the reply. Please ref my other post regarding OData. Once I find a solution to my other OData post and get it working then it would seem sensible to mark your reply as the answer to this post also.
Feb 20, 2016 05:05 PM|Yohann Lu|LINK
From your more detailed description, I suggest you can use batch support and asynchronous methods in the API.
1: batch support
It accepts mime/multipart requests containing multiple HTTP Requests and processes all the requests, sending a mime/multipart response containing the individual responses. You can add your loop processing data to the request set. In this way, just deal with
it once and you can finish it.
//Create some requests to query
HttpRequestMessage queryCustomers = new HttpRequestMessage(HttpMethod.Get, "http://localhost:13245/api/test");
HttpMessageContent queryContent = new HttpMessageContent(queryCustomers);
MultipartContent content = new MultipartContent("mixed", "batch_" + Guid.NewGuid().ToString());
Implementation of asynchronous methods in API Web
A substantial increase in the number of processing Http requests for API Web using asynchronous API Web services. Implementation is simple, just use the async keyword and the return value of your method can be changed to Task.
public async Task<IHttpActionResult> Post([FromBody] MEASURE_CAPTURE1 entity)
//Store each record to the database
return Content(HttpStatusCode.OK, entity);
The following tutorial for your reference.
Introducing batch support in Web API and Web API OData: