Access Request QueryString Value In Asp.Net Core 8 MVC Using C#.Net

How to retrieve or access or get request querystring value in asp.net core mvc application using c#.net in controller and display in view or save.
In today's article I will show you how you can get or retrieve or access the request querystring value at controller end in your asp.net core 8 mvc application using c#.net. I will show an example on how to access the string and the integer value at your controller and display the value in the view by storing value in the ViewBag.
What is request querystring in asp..net core mvc? Request QueryString in web application is the value passed in the url with a key identifier. With the help of querystring we cna pass value from one page to anotehr page. In querystring the ? symbol is act as starting point and & symbal we used to pass multiple values. Eg. https://www.example.com/user/index/?Id=1&dataval=testvalue
So, for this article first we will create a new asp.net core8 mvc application and add a controller class file in it. 
[HttpGet]
public IActionResult Index()
{   
      return View();
}
let's take an example. Consider below the below from which we want to access the value. Here the key is id and title. 

http://localhost:5072/?id=1000&title=this is an example

In above URL I have passed two values with key Id and title. Now lets make changes in controller code which will help us to capture the request query string value. After capturing the value, we will pass it to viewbag to display it in view. 
[HttpGet]
public IActionResult Index(int id,string title)
{
    ViewBag.Id=id;
    ViewBag.Title=title;
    return View();
}
In above code i have assigned the querystring value to respective viewbag. After this we will create a view and add the below code to display the querystring value in view.
@{
    ViewData["Title"] = "Home Page";
}
<h2>Query String Value</h2>
<br />
<div>
    <span> Value 1:</span>@ViewBag.Id
    <br />
    <span> Value 2:</span>@ViewBag.Title;
</div>
Now let's run the code and check the output. Here is the value one which is id=1000

Access Request QueryString Value In Asp.Net  Core 8 MVC Using C#.Net

Second value is title which is title=this is an example

Access Request QueryString Value In Asp.Net  Core 8 MVC Using C#.Net

Now we check the final output.

Access Request QueryString Value In Asp.Net  Core 8 MVC Using C#.Net

Post a Comment