asp.net-mvc 在 MVC 控制器中访问 GET 参数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5940112/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Access GET parameters in MVC Controller
提问by Tom
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/1001Now It has to be http://url.com/product/1001?type=1
我开发了一个 MVC 应用程序,现在我需要进行一些更改。我想传递额外的参数,但 URL 的格式无法更改。最初 URL 看起来像http://url.com/product/1001现在它必须是http://url.com/product/1001?type=1
How do I parse type=1 in my Controller module. Kindly help
如何在我的控制器模块中解析 type=1。请帮忙
回答by Tejs
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).
如果命名匹配并且可以进行转换(因此int?也是 的有效类型type),Route、QueryString、Form 和其他值会自动绑定到操作方法签名。
If you don't want to do that, you can always fall back to the ever reliable Request.QueryString[]NameValueCollection.
如果您不想这样做,您可以随时使用可靠的Request.QueryString[]NameValueCollection。
string type = Request.QueryString["type"];

