Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Access GET parameters in MVC Controller [duplicate]

I developed an MVC application and now I need to make some changes. I would like to pass additional parameters and the format of URL cannot be changed. Initially the URL looked like http://url.com/product/1001 Now It has to be http://url.com/product/1001?type=1

How do I parse type=1 in my Controller module. Kindly help

like image 757
Tom Avatar asked May 09 '11 17:05

Tom


1 Answers

You can simply add it to the action method signature:

 public ActionResult MyMethod(string type)
 {

 }

Route, QueryString, Form, and other values automatically get bound to action method signatures if the naming matches and a conversion is possible (so int? would also be a valid type for type).

If you don't want to do that, you can always fall back to the ever reliable Request.QueryString[] NameValueCollection.

string type = Request.QueryString["type"];
like image 109
Tejs Avatar answered Oct 15 '22 13:10

Tejs