In our previous article we saw how to implement a Get method to read data from the Web API. You can read it here.
Asynchronous Controller of Web API 2 With Entity Framework 6: Get Method
This article explains how to do a Post operation to send data from a .NET client to the Web API. We are using the Entity Framework 6 to do a data save operation asynchronously. Here is the controller class where we have implemented the Post() method.
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Web.Http;
- using System.Web.Mvc;
- using System.Threading.Tasks;
- using System.Web.Http.Description;
-
- namespace WebAPI.Controllers
- {
- public class AsyncPersonController : ApiController
- {
- [ResponseType(typeof(company))]
- public async Task<IHttpActionResult> Post(company com)
- {
- using (var ctx = new efDBEntities())
- {
- ctx.company.Add(com);
- await ctx.SaveChangesAsync();
- return CreatedAtRoute("DefaultApi", new {id=com.cid}, com);
- }
- }
- }
- }
It will return the newly created object in the response. Fine, we will now development the client part of the entire application that will send one object of the emppany entity. Have a look at the following example.
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using ClassLibrary;
- using System.Threading;
- using System.Net.Http;
- using System.Net.Http.Headers;
- using System.Net.Http.Formatting;
- using Newtonsoft.Json;
-
- namespace ConsoleAPP
- {
- public class companyEntity
- {
- public int c_id { get; set; }
- public string company_name { get; set; }
- }
- class Program
- {
- static void Main(string[] args)
- {
- using (var client = new HttpClient())
- {
-
- companyEntity c = new companyEntity { company_name = "TCS" };
-
- client.BaseAddress = new Uri("http://localhost:1565/");
- var response = client.PostAsJsonAsync("api/AsyncPerson/", c).Result;
- var data = response.Content;
-
- if (response.IsSuccessStatusCode)
- {
- Task<string> d = data.ReadAsStringAsync();
- Console.Write(d.Result.ToString());
- }
- Console.ReadLine();
- }
- }
- }
- }
The same class we have designed in our client end that is defined by the Entity Framework in the Web API end. Now, we will use the PostAsJsonAsync() function to send data asynchronously. The point to be noted is this call should be asynchronous; I mean by using the “await” keyword. For that the container function should be decorated with the “async” keyword, since this is the Main() of the console application we are unable to decorate it but in a real application you should call it asynchronously . Once we run the application we will see that the new object is created in the DB has thrown in the client end.
And one new record has been entered into the DB too.
Finally
If you have some serialization issues in the client end then please ensure that you have installed the Web API 2.1 core package from the Nugget Package Manager. Thanks for reading, Happy learning.