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.
      Web Application
    • From the "MVC4 Project" window select "Web API".
      MVC4 Project
    • Click on the "OK" button.
  2. In the view add some code.
    • In the "Solution Explorer".
    • Expand the "Views" folder.
    • Select "Home" -> "Index.cshtml".
      Solution Explorer
    • Add the following code
      @{
          ViewBag.Title = "Index";
      }
      
      <h3>Passing multiple parameters in URL</h3>
      
      @using (Html.BeginForm("index", "Home"))
      {
          <a href="/home/MultipleParameter/?data1=678&data2=c-sharpcorner">Click here for passing the parameter</a>
      }
      
      In the code above there is a hyperlink, in it, we pass the two parameters "data1" and "data2". In this URL is defined as "home" which is the controller name and "MultipleParameter" which 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".
      HomeController
    • Add the following code to create the Action Method.
      public ActionResult MultipleParameter(int data1, string data2)
      {
          ViewData["Data1"] = data1;
          ViewData["Data2"] = data2;
          return View();
      }
      
      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.
      using System;
      using System.Collections.Generic;
      using System.Linq;
      using System.Web;
      using System.Web.Mvc;
      
      namespace MultipleParameterAPI.Controllers
      {
          public class HomeController : Controller
          {
              public ActionResult Index()
              {
                  return View();
              }
      
              public ActionResult MultipleParameter(int data1, string data2)
              {
                  ViewData["Data1"] = data1;
                  ViewData["Data2"] = data2;
                  return View();
              }
          }
      }
      
  4. Now create a View as in the following.
    • In the "HomeController".
    • Right-click on the "MultipleParameter" Action method.
    • Select "AddView".
      AddView
    • Click on the "Add" button.
      Add button
    • Add the following code in this view.
      @{
          ViewBag.Title = "MultipleParameter";
      }
      
      <h2>MultipleParameter</h2>
      
      <h3><u>Values of multiple parameter is received</u></h3>
      
      <br />
      
      <h3>Data1: @ViewData["Data1"]</h3>
      
      <br />
      
      <h3>Data2: @ViewData["Data2"]</h3>
      
  5. Now execute the application.
    Execute the application
  6. Click on the link. To see the parameter value insert a breakpoint on the MultipleParameter action method.
    Parameter value
    MultipleParameter action
  7. The output will be,
    Output


Similar Articles