8

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

0

1 Answer 1

25

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"];
Sign up to request clarification or add additional context in comments.

1 Comment

I made the following change public ActionResult Product(int id, int type) {} But I was unable to do a successful parse. I think I'm doing something wrong

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.