• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
  • Skip to footer
Email: info@dotnettec.com | Mobile: +91-8800268750

DotNetTec

  • Home
  • Tutorials
    • Programming Language
      • C#
      • JavaScript
    • .Net Development
      • .NET Framework
      • ASP.NET Web Forms
      • MVC Architecture
      • Web API
      • .NET Core Framework
    • Front End Frameworks
      • JavaScript
      • HTML
    • Cloud Computing
      • Microsoft Azure
    • Database
      • SQL Server
    • Google Maps API
    • RPA
      • Blue Prism
      • UiPath
  • How to
  • Knowledge Base
  • Shop

Pass Multiple Parameters in Web API URL C#

April 13, 2020 by Ravi Kumar Leave a Comment

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.

add class library

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.

multiple object array list

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;
            }
        }
    }

Download Source Code

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.

Category iconASP.NET Web API,  How to Tag iconpass multiple parameters in web api,  pass multiple parameters in web api get url c#,  pass multiple parameters in web api post url c#

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

Recent Posts

  • Google Maps Draw Polygon Get Coordinates
  • Check if point inside polygon Google Maps JavaScript
  • How to Create a Calendar Control in ASP.NET
  • Interview Etiquette Before, During and After
  • How to Attend Telephonic Interview Tips

Products

  • Online Shopping ASP.NET Project Source Code Online Shopping ASP.NET Project Source Code
    Rated 5.00 out of 5
    $69.00 $39.00
  • responsive-aspnet-mvc-grid-view ASP.NET MVC full Ajaxify and Bootstrap Grid with CRUD Operation
    Rated 4.67 out of 5
    $21.00 $9.00
  • eCommerce Website Source Code in ASP.NET eCommerce Website Source Code in ASP.NET $99.00
  • Hospital Management System Project ASP.Net Source Code
    Rated 5.00 out of 5
    $99.00
  • Whatsapp Bulk Message Sender Source Code WhatsApp Bulk Message Sender Source Code
    Rated 5.00 out of 5
    $69.00

Footer

DotNetTec

A platform for Software Developers and Architects. Learn new skills and apply them in real-life to build an end to end applications.

Sitemap

  • About
  • Blog
  • Shop
  • Contact
  • Privacy Policy

Expertise

  • Web Development
  • Custom Software Development
  • Web API Development
  • Google Maps APIs
  • Facebook
  • Instagram
  • Pinterest
  • Twitter
  • YouTube

Copyright © 2021 DotNetTec. All rights reserved. Return to top