Handling ASP.NET Web API Query String Parameters | CodeGuru

Handling ASP.NET Web API Query String Parameters

Introduction Parameters passing in the query string of an ASP.NET API GET request don’t really pass in a general fashion; parameters have to be specified explicitly in the routing configuration methods. The parameters you specify on your controller action get mapped to parameters sent in the query string of a GET request. In this article, […]

Written By
Tapas Pal
Tapas Pal
Mar 19, 2019
2 minute read
CodeGuru content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More

Introduction

Parameters passing in the query string of an ASP.NET API GET request don’t really pass in a general fashion; parameters have to be specified explicitly in the routing configuration methods. The parameters you specify on your controller action get mapped to parameters sent in the query string of a GET request. In this article, I will describe how to pass multiple parameters in a query string of a Web API URL.

Sample Web API Application

First, we have to create a Web API application. For that, start Visual Studio 2017; from the start window, select “New Project.” From the new project window, select “Installed” -> “Visual C#” -> “Web”. Select “ASP.NET MVC Web Application” and click the “OK” button. This is shown in Figure 1.

New ASP.NET MVC Application
Figure 1: New ASP.NET MVC Application

From the “MVC Project” window, select “Web API.” Make sure the Razor view engine is selected. Click the ‘OK’ button (see Figure 2).

ASP.NET Web API Template
Figure 2: ASP.NET Web API Template

Next, we will add some code in the index view. From the “Solution Explorer,” expand the “Views” folder and Select “Home” -> “Index.cshtml”. See this in Figure 3.

ASP.NET Solution Explorer
Figure 3: ASP.NET Solution Explorer

@{
   ViewBag.Title = "Index";
}

<h3>Passing multiple parameters in Web API URL</h3>

@using (Html.BeginForm("index", "Home"))

{
   <a href="/home/MultipleQueryParameter/?myparam1=1234567
      &myparam2=TapasPal">Click here to pass the parameter</a>
}

In the preceding code there, is a hyperlink; in it, I have passed the two parameters, “myparam1” and “myparam2”. In the URL defined “home” are the controller name and “MultipleQueryParameter” that is the Action Name defined in the HomeController.

Next, we have to open the “HomeController” Controller and create a new Action Method. For that, in the “Solution Explorer” -> Expand the “Controller” folder ->, select “HomeController”, as shown in Figure 4.

ASP.NET Solution Explorer—Home Controller
Figure 4: ASP.NET Solution Explorer—Home Controller

Now, I have added the following code to create the Action Method:

public ActionResult MultipleQueryParameter(int myparamdata1,
   string myparamdata2)
{
   ViewData["data1"] = myparamdata1;
   ViewData["data2"] = myparamdata2;
   return View();
}

In this action method, I have defined two parameters for displaying the value of the parameter. The value of the parameter is assigned to the “ViewData” parameter.

The code snippet of the “HomeController” looks like the following:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace MVCWebAPI.Controllers
{
   public class HomeController : Controller
   {
      public ActionResult Index()
      {
         ViewBag.Title = "Home Page";
         return View();
      }

      public ActionResult MultipleQueryParameter(int myparamdata1,
         string myparamdata2)
      {
         ViewData["data1"] = myparamdata1;
         ViewData["data2"] = myparamdata2;
         return View();
      }
   }
}

Next, I have created a View to display query string values. To create the view, in “HomeController”, right-click the “MultipleParameter” Action method. Select “AddView” (see Figure 5).

ASP.NET Solution—Add New View
Figure 5: ASP.NET Solution—Add New View

Click the “Add” button, as you can see in Figure 6.

ASP.NET Solution—New Razor View
Figure 6: ASP.NET Solution—New Razor View

@{

   ViewBag.Title = "MultipleQueryParameter";

}

<h2>MultipleParameter</h2>

<h3><u>Values of multiple parameters are received</u></h3>

<br />

<h3>Data1:@ViewData["data1"]</h3>

<br />

<h3>Data2:@ViewData["data2"]</h3>

Finally, I have executed the application to see output.

Advertisement

Conclusion

That was all about ASP.NET Web API query string parameters. I hope this article was helpful!! That’s all for today; happy reading!

CodeGuru Logo

CodeGuru covers topics related to Microsoft-related software development, mobile development, database management, and web application programming. In addition to tutorials and how-tos that teach programmers how to code in Microsoft-related languages and frameworks like C# and .Net, we also publish articles on software development tools, the latest in developer news, and advice for project managers. Cloud services such as Microsoft Azure and database options including SQL Server and MSSQL are also frequently covered.

Property of TechnologyAdvice. © 2026 TechnologyAdvice. All Rights Reserved

Advertiser Disclosure: Some of the products that appear on this site are from companies from which TechnologyAdvice receives compensation. This compensation may impact how and where products appear on this site including, for example, the order in which they appear. TechnologyAdvice does not include all companies or all types of products available in the marketplace.