Pass Multiple Parameters in URL in Web API

Introduction

This article describes how to pass multiple parameters in a query string or URL Here pass the parameter in the URL.

  1. First create a Web API Application.
    • Start Visual Studio 2012.
    • From the start window select "New Project".
    • From the new project window select "Installed" -> "Visual C#" -> "Web".
    • Select "ASP.NET MVC4 Web Application" and click on the "OK" button.

      mult.jpg
    • From the "MVC4 Project" window select "Web API".

      mult1.jpg
    • Click on the "OK" button.
  2. In the view add some code.
    • In the "Solution Explorer".
    • Expand the "Views" folder.
    • Select "Home" -> "Index.cshtml".

      mult5.jpg

      Add the following code:
      1. @{      
      2.     ViewBag.Title = "Index";      
      3. }      
      4. <h3>Passing multiple parameter in URL</h3>      
      5. @using (Html.BeginForm("index""Home"))      
      6. {      
      7.  <a href="/home/MultipleParameter/?data1=678&data2=c-sharpcorner">Click here for passing the parameter</a>        
      8. }
      In the code above there is a hyperlink, in it we pass the two parameters "data1" and "data2". And in this URL is defined "home" that is the controller name and "MultipleParameter" that is the Action Name that is defined in the HomeController.
  3. Now return to the "HomeController" Controller and create a new Action Method.

    • In the "Solution Explorer".

    • Expand the "Controller" folder.

    • Select "HomeController".

      mult2.jpg

      Add the following code to create the Action Method:

      1. public ActionResult MultipleParameter(int data1, string data2)    
      2. {    
      3.     ViewData["Data1"] = data1;    
      4.     ViewData["Data2"] = data2;    
      5.     return View();    
      6. }  
      In this action method we defined two parameters for displaying the value of the parameter. The value of the parameter is assigned to the "ViewData".

      The entire code of the "HomeController" looks like: 
      1. using System;    
      2. using System.Collections.Generic;    
      3. using System.Linq;    
      4. using System.Web;    
      5. using System.Web.Mvc;    
      6. namespace MultipleParameterAPI.Controllers    
      7. {    
      8.     public class HomeController : Controller    
      9.     {    
      10.         public ActionResult Index()    
      11.         {    
      12.             return View();    
      13.         }    
      14.         public ActionResult MultipleParameter(int data1, string data2)    
      15.         {    
      16.             ViewData["Data1"] = data1;    
      17.             ViewData["Data2"] = data2;    
      18.             return View();    
      19.         }    
      20.     }    
      21. }

       

  4. Now create a View as in the following.

    • In the "HomeController".

    • Right-click on the "MultipleParameter" Action method.

    • Select "AddView".

      mult3.jpg

    • Click on the "Add" button.

      mult4.jpg

      Add the following code in this view:

      1. @{    
      2.     ViewBag.Title = "MultipleParameter";    
      3. }    
      4. <h2>MultipleParameter</h2>    
      5. <h3><u>Values of multiple parameter is received</u></h3>    
      6. <br />    
      7. <h3>Data1 :@ViewData["Data1"]</h3>    
      8. <br />    
      9. <h3>Data2 :@ViewData["Data2"]</h3>

       

  5. Now execute the application.

    mult8.jpg

Click on the link. To see the parameter value insert a breakpoint on the MultipleParameter action method.

mult6.jpg

mult7.jpg

The output will be:

mult9.jpg


Similar Articles