Query String Generating In MVC Using Razor Syntex

A query string is the part of a uniform resource locator (URL) containing data that does not fit conveniently into a hierarchical path structure. The query string commonly includes fields added to a base URI by a Web browser or other client application, for example as part of an HTML form.

Query strings can be generated by form submission, or by a user typing a query into the address bar of the browser. Note: If you want to send large amounts of data (beyond 100 kb) the request Query String cannot be used.

Example: https://www.google.com/search?q=query
This Url contains the query string. Query data for q is query.

Option 1:Reading Query Data from MVC Route
If your MVC Project Route URL configure as-
url: “{controller}/{action}/{id}
In view Create your querystring url by-
Html.ActionLink(“Demolink”,”Demo“, “Demos“, new { id = “MVC” })
So your query string looks like
http://loopcoder.com/Demos/Demo/MVC
Then from route url format here your query key is id and your value for key “id” from this url is “MVC”. Finally Reading string from your controller code-
Url.RequestContext.RouteData.Values[“id”]

Option 2:Reading Query Data from Normal Query URL

In view Create your querystring url by-
Html.ActionLink(“Demolink”,”Demo“, “Demos“, new { id = “id”, pid = “MVC” } })
So your query string looks like
http://loopcoder.com/Demos/Demo/id?pid=MVC
Then from url here your query key is pid and your value for key “pid” from this url is “MVC”. Finally Reading string from your controller code-
Request.QueryString[“pid”]

Leave a Reply

Your email address will not be published. Required fields are marked *