In this article I will explain with an example, how to use Request.Query collection for extracting QueryString parameter values in ASP.Net Core MVC (.Net Core).
Note: For beginners in ASP.Net Core (.Net Core 7), please refer my article ASP.Net Core 7: Hello World Tutorial with Sample Program example.
 
 

Model

The Model class consists of following properties.
public class PersonModel
{
    ///<summary>
    /// Gets or sets PersonId.
    ///</summary>
    public int PersonId { getset; }
 
    ///<summary>
    /// Gets or sets Name.
    ///</summary>
    public string Name { getset; }
 
    ///<summary>
    /// Gets or sets Gender.
    ///</summary>
    public string Gender { getset; }
 
    ///<summary>
    /// Gets or sets City.
    ///</summary>
    public string City { getset; }
}
 
 

Controllers

Source Controller

The Controller consists of following Action methods.

Action method for handling GET operation

Inside this Action method, simply the View is returned.
 

Action method for handling POST operation

This Action method gets called when Send Button is clicked or when the Form is submitted.
Note: For details about Form Post in ASP.Net Core MVC, please refer my article ASP.Net Core: Form Submit (Post) Example.
 
Inside this Action method, the QueryString Parameters along with its value are passed to the RedirectToAction method which redirects the page to PersonDetails controller.
public class HomeController : Controller
{
    public IActionResult Index()
    {
        return View();
    }
 
    [HttpPost]
    public IActionResult Send()
    {
        return RedirectToAction("Index""PersonDetails"new { PersonId = 1, Name = "Mudassar Khan", Gender = "Male", City = "Mumbai" });
    }
}
 

Destination Controller (PersonDetails)

The Controller consists of following Action method.

Action method for handling GET operation

Inside this Action method, the QueryString Parameters are extracted using the Request.Query collection.
Finally, the extracted QueryString parameter values are then set into an object of PersonModel class and is return to the View.
public class PersonDetailsController : Controller
{
    public IActionResult Index()
    {
        PersonModel person = new PersonModel
        {
            PersonId = int.Parse(Request.Query["PersonId"]),
            Name = Request.Query["Name"],
            Gender = Request.Query["Gender"],
            City = Request.Query["City"]
        };
        return View(person);
    }
}
 
 

Views

HTML Markup

The View consists of an HTML Form which has been created using the Html.BeginForm method with the following parameters.
asp-action – Name of the Action. In this case the name is Send.
asp-controller – Name of the Controller. In this case the name is Home.
method – It specifies the Form Method i.e. GET or POST. In this case it will be set to POST.
The Form consists of a Submit Button which when is clicked, the Form is submitted.
@addTagHelper*, Microsoft.AspNetCore.Mvc.TagHelpers
@{
    Layout = null;
}
 
<!DOCTYPE html>
 
<html>
<head>
    <meta name="viewport" content="width=device-width"/>
    <title>Index</title>
</head>
<body>
    <form method="post" asp-controller="Home" asp-action="Send">
        <input type="submit" value="Send" />
   </form>
</body>
</html>
 

Destination View (PersonModel)

Inside the View, in the very first line the PersonModel class is declared as Model for the View.
The View consists of an HTML Table which is used for displaying the detail of Person using Model.
@model Request_QueryString_Core.Models.PersonModel
@{
    Layout = null;
}
 
<!DOCTYPE html>
 
<html>
<head>
    <meta name="viewport" content="width=device-width"/>
    <title>Index</title>
</head>
<body>
    <table cellpadding="0" cellspacing="0">
        <tr>
            <th colspan="2" align="center">Person Details</th>
        </tr>
        <tr>
            <td>PersonId: </td>
            <td>@Model.PersonId</td>
        </tr>
        <tr>
            <td>Name: </td>
            <td>@Model.Name</td>
        </tr>
        <tr>
            <td>Gender: </td>
            <td>@Model.Gender</td>
        </tr>
        <tr>
            <td>City: </td>
            <td>@Model.City</td>
        </tr>
    </table>
</body>
</html>
 
 

Screenshot

Using Request.Query in ASP.Net Core
 
 

Downloads