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.
  1. In the view add some code.
  • In the "Solution Explorer".
  • Expand the "Views" folder.
  • Select "Home" -> "Index.cshtml".

mult5.jpg

Add the following code:

@{

    ViewBag.Title = "Index";

}

<h3>Passing multiple parameter 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". 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.

  1. 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:

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();

        }

    }

}

 

  1. 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:

@{

    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>

  1. 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