As I’ve looked this up three times now I should put the code directly on the blog. Happily Microsoft has done a lot of the plumbing work for you.
You have a Web API controller with an action that looks like this
[System.Web.Http.Route("Ajax/v1.1/PostItemBatch")]
[OutputCache(NoStore = true, Duration = 0)]
[System.Web.Http.HttpPostAttribute]
public bool PostItemBatch(List postItems)
{
using (ApplicationDbContext db = new ApplicationDbContext())
{
foreach (PostItem postItem in postItems)
{
db.PostItems.Add(postItem);
}
db.SaveChanges();
return true;
}
}
Then you can post data to it by the following
List PostItems = new List();
for (int i = 0; i < 11; i++)
{
PostItems.Add(new PostItem(true));
}
HttpClient client = new HttpClient();
HttpResponseMessage response = client.PostAsJsonAsync("http://localhost:56168/Ajax/v1.1/PostPostItemBatch", PostItems).Result;
var test = response.IsSuccessStatusCode;
And there you go!
|
Written By Steve French |
Leave a Reply