ASP.NET MVC - Getting QueryString Values

In ASP.NET MVC do you have to select QueryString parameters in the same way as in ASP.NET WebForms? or is any declaration used [AcceptVerbs (HttpVerbs.Get)]?

+52
asp.net-mvc
Mar 09 '09 at 20:13
source share
5 answers

Query string parameters can be accepted simply with an argument in action - i.e.

public ActionResult Foo(string someValue, int someOtherValue) {...} 

which will accept the request, for example ... / someroute? someValue = abc & someOtherValue = 123

In addition, you can see the request directly for more control.

+77
Mar 09 '09 at 20:17
source share

You can always use the Request.QueryString collection, such as web forms, but you can also force MVC to process them and pass them as parameters. This is easiest to suggest, and it automatically checks the type of input.

+23
Mar 09 '09 at 20:16
source share

I think you are looking

 Request.QueryString["QueryStringName"] 

and you can access it by views by adding @

Now look at my example, I generated Url with QueryString

  var listURL = '@Url.RouteUrl(new { controller = "Sector", action = "List" , name = Request.QueryString["name"]})'; 

listURL value is /Sector/List?name=value'

and when queryString empty

Value listURL /Sector/List

+13
Oct. 27 '16 at 12:34
source share

I recommend using the controller’s ValueProvider property, basically the way UpdateModel / TryUpdateModel does it to retrieve the necessary route, query, and form parameters. This will lead to the fact that the signatures of your methods will grow very large and will be subject to frequent changes. This also makes testing easier, since you can put a ValueProvider on the controller during unit tests.

+7
Mar 09 '09 at 20:24
source share

In fact, you can capture query strings in MVC in two ways .....

 public ActionResult CrazyMVC(string knownQuerystring) { // This is the known query string captured by the Controller Action Method parameter above string myKnownQuerystring = knownQuerystring; // This is what I call the mysterious "unknown" query string // It is not known because the Controller isn't capturing it string myUnknownQuerystring = Request.QueryString["unknownQuerystring"]; return Content(myKnownQuerystring + " - " + myUnknownQuerystring); } 

This will capture both query strings ... for example:

 /CrazyMVC?knownQuerystring=123&unknownQuerystring=456 Output: 123 - 456 

Don't ask me why they designed it that way. It would be more prudent if they threw out the entire Controller action system for individual query strings and simply returned a captured dynamic list of all strings / encoded file objects for the URL using url-encoding so you can easily access them in one times, Maybe someone here can demonstrate that, if possible?

It doesn't make any sense to me how controllers capture query strings, but that means you have more flexibility to capture query strings than they teach you out of the box. So take your poison .... both work great.

0
Jul 03 '17 at 4:25
source share



All Articles