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. publicclassProduct
  2. {
  3. publicintProductId{ get;set;}
  4. public string Name{ get;set;}
  5. public string Category{ get;set;}
  6. public decimal Price{ get;set;}
  7. }
  8.  
  9. publicclassSupplier
  10. {
  11. publicintSupplierId{ 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. publicclassHomeController:Controller
  2. {
  3. publicActionResultIndex()
  4. {
  5. HttpClient client =newHttpClient();
  6. Uri baseAddress =newUri(http://localhost:2939/”);
  7. client.BaseAddress= baseAddress;
  8. ArrayList paramList =newArrayList();
  9. Product product =newProduct{ProductId=1,Name=“Book”,Price=500,Category=“Soap”};
  10. Supplier supplier =newSupplier{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. returnView();
  17. }
  18. else
  19. {
  20. returnRedirectToAction(“About”);
  21. }
  22. }
  23. publicActionResultAbout()
  24. {
  25. returnView();
  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. publicclassProductController:ApiController
  2. {
  3. [ActionName(“SupplierAndProduct”)]
  4. [HttpPost]
  5. publicHttpResponseMessageSuppProduct(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 =newHttpResponseMessage{StatusCode=HttpStatusCode.Created};
  13. return response;
  14. }
  15. else
  16. {
  17. HttpResponseMessage response =newHttpResponseMessage{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. publicclassHomeController:Controller
  2. {
  3. publicActionResultIndex()
  4. {
  5. HttpClient client =newHttpClient();
  6. Uri baseAddress =newUri(http://localhost:2939/”);
  7. client.BaseAddress= baseAddress;
  8. JArray paramList =newJArray();
  9. Product product =newProduct{ProductId=1,Name=“Book”,Price=500,Category=“Soap”};
  10. Supplier supplier =newSupplier{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. returnView();
  17. }
  18. else
  19. {
  20. returnRedirectToAction(“About”);
  21. }
  22. }
  23. publicActionResultAbout()
  24. {
  25. returnView();
  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. publicclassProductController:ApiController
  2. {
  3. [ActionName(“SupplierAndProduct”)]
  4. [HttpPost]
  5. publicHttpResponseMessageSuppProduct(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 =newHttpResponseMessage{StatusCode=HttpStatusCode.Created};
  13. return response;
  14. }
  15. else
  16. {
  17. HttpResponseMessage response =newHttpResponseMessage{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.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: