In this web api tutorial, you will learn how to pass multiple parameters in web api URL C#. In the previous article, I have explained How to create Web API in ASP.NET C#, Web API CRUD Operations using ASP.NET MVC and Entity Framework, Calling Web API from MVC Controller Httpclient.
Problem Statement
ASP.Net Web API is focused on resource-based solutions and HTTP verbs. Web API has a limitation while sending data to a Web API Controller. In web API you can pass only a single complex type as a parameter. But sometimes you may need to pass multiple complex types as parameters, how do I implement this?
Pass Multiple Parameters in Web API
There are two different ways to implement passing multiple complex type parameters to asp.net web API. First: Wrapping your multiple classes into a wrapper class and passing this wrapper class as a parameter. The second and simple approach is using an Array List. Let see how to achieve this task. Find the below steps:-
Step1: Create a Blank Solution called WebAPI-Multiple-Objects as shown below. Add a new class library project (Model) into it and create two classes named Product, Supplier.
public class Product { public int ProductId { get; set; } public string Name { get; set; } public decimal Price { get; set; } } public class Supplier { public int SupplierId { get; set; } public string Name { get; set; } public string Address { get; set; } }
Step2: Add a new MVC project and 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-
public class HomeController : Controller { public ActionResult Index() { HttpClient client = new HttpClient(); Uri baseAddress = new Uri("http://localhost:4160/"); client.BaseAddress = baseAddress; ArrayList paramList = new ArrayList(); Product product = new Product { ProductId = 1, Name = "eBook", Price = 700 }; Supplier supplier = new Supplier { SupplierId = 1, Name = "Ravi Kumar", Address = "Noida" }; paramList.Add(product); paramList.Add(supplier); HttpResponseMessage response = client.PostAsync("api/product/SupplierAndProduct", new StringContent( new JavaScriptSerializer().Serialize(paramList), Encoding.UTF8, "application/json")).Result; if (response.IsSuccessStatusCode) { return View(); } else { return RedirectToAction("About"); } } public ActionResult About() { return View(); } }
Now, on the 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-
public class ProductController : ApiController { [ActionName("SupplierAndProduct")] [HttpPost] public HttpResponseMessage SuppProduct(ArrayList paramList) { if (paramList.Count > 0) { Product product = JsonConvert.DeserializeObject<Product>(paramList[0].ToString()); Supplier supplier = JsonConvert.DeserializeObject<Supplier>(paramList[1].ToString()); //TO DO: Your implementation code var response = new HttpResponseMessage { StatusCode = HttpStatusCode.Created }; return response; } else { HttpResponseMessage response = new HttpResponseMessage { StatusCode = HttpStatusCode.InternalServerError }; return response; } } }
Conclusion
I hope you liked this article on passing multiple complex type parameters in web API. I would like to have feedback from my blog readers. Your valuable feedback, question, or comments about this article are always welcome.
Leave a Reply