dcsimg

Handling ASP.NET Web API Query String Parameters

WEBINAR:
On-Demand

Desktop-as-a-Service Designed for Any Cloud ? Nutanix Frame


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!



About the Author

Tapas Pal

I am working in Microsoft Technology for last 15 years and presently working with Cognizant Technology Solutions, India as Senior Architect. I have completed TOGAF 9.1,Microsoft Certification on .NET 1.1 , .NET 2.0, SQL Server 2005 and Sharepoint. Please visit my Blog - http://tapas-pal.blogspot.com/

Related Articles

Comments

  • There are no comments yet. Be the first to comment!

  • You must have javascript enabled in order to post comments.

Leave a Comment
  • Your email address will not be published. All fields are required.

Most Popular Programming Stories

More for Developers

RSS Feeds

Thanks for your registration, follow us on our social networks to keep up-to-date