在 ASP.NET MVC3 应用程序中,我有一个如下所示的函数:
Public Sub DoSomething(controllerCtx As ControllerContext)
....
' Which to use? and Why?
Dim val = controllerCtx.HttpContext.Request.Params.Item("someKey")
Dim val = controllerCtx.HttpContext.Request.Item("someKey")
....
End Sub
(我知道 Item
是两者的 Default
属性,可以删除,这与这个问题无关。)
查看 MSDN 页面 Request.Item
和 Params.Item
,我没有看到任何差异。两个页面都说它们从以下位置获取值:Cookie、Form、QueryString 或 ServerVariables 集合。 (尽管他们列出的订单确实不同。)
我见过this Stack Overflow post ,但似乎答案更多地集中在 QueryString
组件上,而不是 Request.Params.Item
与 Request.Item
之间。
为什么我会使用其中一种而不是另一种?两者有什么区别吗?
最佳答案
两者在内容上是严格等同的。以下是内容和搜索顺序:
- 查询字符串
- 表格
- Cookie
- 服务器变量
至于使用哪一个,在 ASP.NET MVC 应用程序中最好使用:
controllerCtx.Controller.ValueProvider.GetValue("someKey");
因为这也考虑了路由和自定义值提供程序(例如 JsonValueProvider)。但当然,一切都取决于您的场景和具体要求。
关于.net - Request ("key") 和 Request.Params ("key") 之间有什么区别吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7286123/