How to use Querystring Parameters in Asp.Net MVC to Retrieve or Send Data

Here we will learn query stirng parameters in mvc with example. Generally query string is one of client side state management techniques in ASP.NET in which query string stores values in URL that are visible to Users. We mostly use query strings to pass data from one page to another page in mvc.


In mvc routing has a support for query strings in RouteConfig.cs let’s have a look on. Our routing class file RouteConfig.cs  will contain id as optional Parameter.


public static void RegisterRoutes(RouteCollection routes)




name: "Default",

url: "{controller}/{action}/{id}",

defaults: new


controller = "Home",

action = "Index",

id = UrlParameter.Optional



In mvc we can pass query string with Optional Parameter and also without Optional Parameter.


Query strings with Optional Parameter




Query strings without Optional Parameters 



Create New Asp.Net MVC Application

We will learn querystrings with simple example for that Open visual studio studio à Go to File à Select New à Select Project


create new mvc project from visual studio 2012


After that you will see new dialog will pop up for selecting your Template and Project type. From Templates select Visual C# à inside that select Web and then project type select ASP.NET MVC 4 Web Application and here we are giving name as “Tutorial9_Querystring” then finally click on ok button


create new mvc project from visual studio 2012


 After naming it just click on OK now new dialog will pop up for selecting template in that Select Basic template, view enginer as Razor and click ok like as shown below


select basic template to create new application in mvc


After that visual studio will create new project for us based on our configuration that will be like as shown below


after create new mvc application for querystring demo


Now we will create a Controller with Name MyHomeController

Adding New Controller (MyHomeController) in Application

To add controller just right click on Controller folder then select Add from list and inside that select controller.


add new controller in mvc application


After selecting controller a new dialog will popup with name Add Controller in that give name as "MyHomeController". In template we are going to select Empty MVC controller like as shown below


select template to create new controller for query string in mvc


After Crcting controller here is Code snippet of it.


using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.Mvc;


namespace Tutorial9_Querystring.Controllers


public class MyHomeController : Controller



// GET: /MyHome/

public ActionResult Index()


return View();




Creating index ActionMethod [With Optional Parameter]

After adding controller now let's add parameters to index action method. This action method has optional parameter (ID) with 2 other parameter Name and Department.


public ActionResult Index(string ID, string Name, string Department)


return View();


Save Application and Run it

Now let's run application and access URL. For calling this URL first we are going to call Controller [MyHome] then following with Action Method [Index] lastly with Optional Parameter [10] then query string  will be like [? Name=saineshwar&&Department=SoftwareDevelopment  ].


E.g. URL will be like  http://localhost:2151/MyHome/Index/10?Name=saineshwar&&Department=SoftwareDevelopment 


 Below is Completed snapshot of how to pass Query string

Query string format in mvc application


Output after passing Query string [With Optional Parameter].


Output of Query string parameters in mvc with example


Following is the snapshot while debugging


Query string example in debug mode in mvc application

Creating Details ActionMethod [Without Optional Parameter]

Now let’s create Action Method with Name Details and this method will be without optional parameters we are going to pass 2 parameter Name and Department that will be like as shown below 


public ActionResult Details(string Name, string Department)


string result = " Name= " Name " Department= " Department;

return Content(result);


Save Application and Running it

Now lets Run Application and access URL. For calling this URL first we are going to call Controller [MyHome] then following with ActionMethod [Details] then lastly query string that will be like as [? Name=saineshwar&&Department=SoftwareDevelopment]


E.g. URL will be like http://localhost:2151/MyHome/Details?Name=saineshwar&Department=SoftwareDevelopment 


Output of Query string parameters in mvc with example


Following is the snapshot while debugging


Query string example in debug mode in mvc application

Now we completed query string in mvc with accessing from URL now let’s call Action Method from Action Link from passing Query string. Calling Action method from Action Link with Passing Query string. 


With Optional Parameters


@Html.ActionLink("Call Index Action", "Index", new { ID = 10, Name = 3, Department = "test" })

Without Optional Parameters


@Html.ActionLink("Call Details Action", "Details", new { Name = 3, Department = "test" })