Tuesday 25 July 2017

Passing multiple complex type parameters to ASP.NET Web API

Asp.Net Web API introduces a new powerful REST API which can be consume by a broad range of clients including browsers, mobiles, iphone and tablets. It is focused on resource based solutions and HTTP verbs.
Asp.Net Web API has a limitation while sending data to a Web API controller. In Asp.Net Web API you can pass only single complex type as a parameter. But sometimes you may need to pass multiple complex types as parameters, how to achieve this?
You can also achieve this task by wrapping your Supplier and Product classes into a wrapper class and passing this wrapper class as a parameter, but using this approach you need to make a new wrapper class for each actions which required complex types parameters. In this article, I am going to explain another simple approach using ArrayList.
Let's see how to achieve this task. Suppose you have two classes - Supplier and Product as shown below -
  1. public class Product
  2. {
  3. public int ProductId { get; set; }
  4. public string Name { get; set; }
  5. public string Category { get; set; }
  6. public decimal Price { get; set; }
  7. }
  8.  
  9. public class Supplier
  10. {
  11. public int SupplierId { get; set; }
  12. public string Name { get; set; }
  13. public string Address { get; set; }
  14. }
In your Asp.Net MVC controller you are calling your Web API and you need to pass both the classes objects to your Web API controller.

Method 1 : Using ArrayList

For passing multiple complex types to your Web API controller, add your complex types to ArrayList and pass it to your Web API actions as given below-
  1. public class HomeController : Controller
  2. {
  3. public ActionResult Index()
  4. {
  5. HttpClient client = new HttpClient();
  6. Uri baseAddress = new Uri("http://localhost:2939/");
  7. client.BaseAddress = baseAddress;
  8. ArrayList paramList = new ArrayList();
  9. Product product = new Product { ProductId = 1, Name = "Book", Price = 500, Category = "Soap" };
  10. Supplier supplier = new Supplier { SupplierId = 1, Name = "AK Singh", Address = "Delhi" };
  11. paramList.Add(product);
  12. paramList.Add(supplier);
  13. HttpResponseMessage response = client.PostAsJsonAsync("api/product/SupplierAndProduct", paramList).Result;
  14. if (response.IsSuccessStatusCode)
  15. {
  16. return View();
  17. }
  18. else
  19. {
  20. return RedirectToAction("About");
  21. }
  22. }
  23. public ActionResult About()
  24. {
  25. return View();
  26. }
  27. }
Now, on Web API controller side, you will get your complex types as shown below.
Now deserialize your complex types one by one from ArrayList as given below-
  1. public class ProductController : ApiController
  2. {
  3. [ActionName("SupplierAndProduct")]
  4. [HttpPost]
  5. public HttpResponseMessage SuppProduct(ArrayList paramList)
  6. {
  7. if (paramList.Count > 0)
  8. {
  9. Product product = Newtonsoft.Json.JsonConvert.DeserializeObject(paramList[0].ToString());
  10. Supplier supplier = Newtonsoft.Json.JsonConvert.DeserializeObject(paramList[1].ToString());
  11. //TO DO: Your implementation code
  12. HttpResponseMessage response = new HttpResponseMessage { StatusCode = HttpStatusCode.Created };
  13. return response;
  14. }
  15. else
  16. {
  17. HttpResponseMessage response = new HttpResponseMessage { StatusCode = HttpStatusCode.InternalServerError };
  18. return response;
  19. }
  20. }
  21. }

Method 2 : Using Newtonsoft JArray

For passing multiple complex types to your Web API controller, you can also add your complex types to JArray and pass it to your Web API actions as given below-
  1. public class HomeController : Controller
  2. {
  3. public ActionResult Index()
  4. {
  5. HttpClient client = new HttpClient();
  6. Uri baseAddress = new Uri("http://localhost:2939/");
  7. client.BaseAddress = baseAddress;
  8. JArray paramList = new JArray();
  9. Product product = new Product { ProductId = 1, Name = "Book", Price = 500, Category = "Soap" };
  10. Supplier supplier = new Supplier { SupplierId = 1, Name = "AK Singh", Address = "Delhi" };
  11. paramList.Add(JsonConvert.SerializeObject(product));
  12. paramList.Add(JsonConvert.SerializeObject(supplier));
  13. HttpResponseMessage response = client.PostAsJsonAsync("api/product/SupplierAndProduct", paramList).Result;
  14. if (response.IsSuccessStatusCode)
  15. {
  16. return View();
  17. }
  18. else
  19. {
  20. return RedirectToAction("About");
  21. }
  22. }
  23. public ActionResult About()
  24. {
  25. return View();
  26. }
  27. }

Note

Don't forget to add reference of Newtonsoft.Json.dll to your ASP.NET MVC project and WebAPI as well.
Now, on Web API controller side, you will get your complex types within JArray as shown below.
Now deserialize your complex types one by one from JArray as given below-
  1. public class ProductController : ApiController
  2. {
  3. [ActionName("SupplierAndProduct")]
  4. [HttpPost]
  5. public HttpResponseMessage SuppProduct(JArray paramList)
  6. {
  7. if (paramList.Count > 0)
  8. {
  9. Product product = JsonConvert.DeserializeObject(paramList[0].ToString());
  10. Supplier supplier = JsonConvert.DeserializeObject(paramList[1].ToString());
  11. //TO DO: Your implementation code
  12. HttpResponseMessage response = new HttpResponseMessage { StatusCode = HttpStatusCode.Created };
  13. return response;
  14. }
  15. else
  16. {
  17. HttpResponseMessage response = new HttpResponseMessage { StatusCode = HttpStatusCode.InternalServerError };
  18. return response;
  19. }
  20. }
  21. }
In this way, you can easily pass your complex types to your Web API. There are two solution, there may be another one as well.

No comments:

Post a Comment