Asynchronous Controller of Web API 2 With Entity Framework 6: Post Method

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.

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.Http;  
  6. using System.Web.Mvc;  
  7. using System.Threading.Tasks;  
  8. using System.Web.Http.Description;  
  9.   
  10. namespace WebAPI.Controllers  
  11. {  
  12.     public class AsyncPersonController : ApiController  
  13.     {  
  14.         [ResponseType(typeof(company))]  
  15.         public async Task<IHttpActionResult> Post(company com)  
  16.         {  
  17.             using (var ctx = new efDBEntities())  
  18.             {  
  19.                 ctx.company.Add(com);  
  20.                 await ctx.SaveChangesAsync();  
  21.                 return CreatedAtRoute("DefaultApi"new {id=com.cid}, com);  
  22.             }  
  23.         }  
  24.     }  

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.

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Threading.Tasks;  
  6. using ClassLibrary;  
  7. using System.Threading;  
  8. using System.Net.Http;  
  9. using System.Net.Http.Headers;  
  10. using System.Net.Http.Formatting;  
  11. using Newtonsoft.Json;  
  12.   
  13. namespace ConsoleAPP  
  14. {  
  15.     public class companyEntity  
  16.     {  
  17.         public int c_id { getset; }  
  18.         public string company_name { getset; }  
  19.     }  
  20.     class Program  
  21.     {  
  22.         static void Main(string[] args)  
  23.         {  
  24.             using (var client = new HttpClient())  
  25.             {  
  26.   
  27.                 companyEntity c = new companyEntity { company_name = "TCS" };  
  28.   
  29.                 client.BaseAddress = new Uri("http://localhost:1565/");  
  30.                 var response = client.PostAsJsonAsync("api/AsyncPerson/", c).Result;  
  31.                 var data = response.Content;  
  32.    
  33.                 if (response.IsSuccessStatusCode)  
  34.                 {  
  35.                     Task<string> d = data.ReadAsStringAsync();  
  36.                     Console.Write(d.Result.ToString());  
  37.                 }  
  38.                 Console.ReadLine();  
  39.             }  
  40.         }  
  41.     }  

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.

object

And one new record has been entered into the DB too.
 
DB

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.

Up Next
    Ebook Download
    View all
    Learn
    View all