.net - Request ("key") 和 Request.Params ("key") 之间有什么区别吗?

标签 .net asp.net-mvc-3 httprequest

在 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.ItemParams.Item ,我没有看到任何差异。两个页面都说它们从以下位置获取值:Cookie、Form、QueryString 或 ServerVariables 集合。 (尽管他们列出的订单确实不同。)

我见过this Stack Overflow post ,但似乎答案更多地集中在 QueryString 组件上,而不是 Request.Params.ItemRequest.Item 之间。

为什么我会使用其中一种而不是另一种?两者有什么区别吗?

最佳答案

两者在内容上是严格等同的。以下是内容和搜索顺序:

  1. 查询字符串
  2. 表格
  3. Cookie
  4. 服务器变量

至于使用哪一个,在 ASP.NET MVC 应用程序中最好使用:

controllerCtx.Controller.ValueProvider.GetValue("someKey");

因为这也考虑了路由和自定义值提供程序(例如 JsonValueProvider)。但当然,一切都取决于您的场景和具体要求。

关于.net - Request ("key") 和 Request.Params ("key") 之间有什么区别吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7286123/

相关文章:

c# - Razor 语法缺少 } 字符

web-services - 改善 CFHTTP 响应时间

c# - 在时区之间转换时间

.net - F# 和 .Net 版本

jquery - KnockoutJS postJSON 方法为何转义我的字符串?

c# - 如何从 mvc3 中的 Html.ActionLink 中废弃 Controller 名称

c# - 如何重写泛型方法?(C#)

.net - 如何在.net 中处理一个类?

Android 无法使用 Volley 库访问 URL

ios - 带有签名请求的 Swift Alamofire 文件上传 : how to send authorization headers?