访问MVC控制器中的GET参数

Tom*_*Tom 9 model-view-controller asp.net-mvc get asp.net-mvc-2

我开发了一个MVC应用程序,现在我需要进行一些更改.我想传递其他参数,并且无法更改URL的格式.最初的网址看起来像http://url.com/product/1001 现在它必须是http://url.com/product/1001?type=1

如何在Controller模块中解析type = 1.请帮助

Tej*_*ejs 24

您只需将其添加到操作方法签名即可:

 public ActionResult MyMethod(string type)
 {

 }
Run Code Online (Sandbox Code Playgroud)

如果命名匹配并且可以进行转换(例如,int?也可以是有效类型type),则Route,QueryString,Form和其他值将自动绑定到操作方法签名.

如果您不想这样做,您可以随时回到可靠的Request.QueryString[]NameValueCollection.

string type = Request.QueryString["type"];
Run Code Online (Sandbox Code Playgroud)