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

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!

More by Author

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Must Read