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.
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.http://localhost:5072/?id=1000&title=this is an example
[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