The community is working on translating this tutorial into Italian, but it seems that no one has started the translation process for this article yet. If you can help us, then please click "More info".
Query String (GET data)
One of the most common ways of taking input from the user, in a web application, is through the query string. In older technologies, like PHP and ASP Classic, you would deal directly with the query string, but as mentioned previously, ASP.NET MVC has abstracted most query string handling into parameters for your Controller actions. However, in some situations, it will still be relevant to read directly from the query string, and fortunately for us, it's still very easy to do in ASP.NET MVC.
What is the query string?
URL's are made up of several parts, like protocol, hostname, path and so on. The query string is the part of the URL that comes after a question-mark character. So, in a URL like this:
Everything after the ? character is considered the query string. In this case, there are two parameters: One called q and one called oq. They have the values "test" and "hello". These would be relevant to the page displayed by the URL - for instance, the q parameter is likely used to tell the server what the user just searched for. The query string is usually appended as the result of the user clicking on a link or submitting a FORM. This allows the same file on your server to handle multiple situations - it can vary the output returned based on the input through the query string.
Accessing the query string
Access to the query string in ASP.NET MVC is very simple - just use the HttpContext class, which this entire chapter is about. The information about the query string is located using one of the following properties: HttpContext.Request.QueryString and HttpContext.Request.Query. The difference is that the QueryString is basically the raw text string, while the Query property allows you to easily access keys and their values. We'll use the Query property in the next examples, where I'll demonstrate just how easy it is to work with:
public class HomeController : Controller
public IActionResult QueryTest()
string name = "John Doe";
name = HttpContext.Request.Query["name"];
return Content("Name from query string: " + name);
The page could now be called with an URL like this one, to reflect the name of the user:
As you can see, I simply check for the existence of a query string parameter with the name of "name", and if it exists, I read it into the variable.
Accessing the query string in your ASP.NET MVC project is still easy, thanks to the HttpContext class.