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

cki*_*tel 2 .net httprequest asp.net-mvc-3

在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
Run Code Online (Sandbox Code Playgroud)

(我知道这Item是一个Default在这两个属性,可以拆卸,这不是在这个问题有关.)

纵观MSDN页Request.ItemParams.Item,我没有看到任何差别.两个页面都说它们从以下值获取值:Cookie,Form,QueryString或ServerVariables集合.(尽管他们确实以不同方式列出了订单.)

我已经看到了这个堆栈溢出职位,但似乎主要集中在答案QueryString组件moreso比Request.Params.ItemVS Request.Item.

为什么我会使用一个而不是另一个?两者之间有什么区别吗?

Dar*_*rov 5

这两者在内容上完全相同.这里是内容和搜索顺序:

  1. 请求参数
  2. 形成
  3. 饼干
  4. ServerVariables

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

controllerCtx.Controller.ValueProvider.GetValue("someKey");
Run Code Online (Sandbox Code Playgroud)

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