2011-09-02 39 views
2

在ASP.NET MVC3應用程序,我有一個看起來像一個功能:Request(「key」)和Request.Params(「key」)之間是否有區別?

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性,可移除,這不是在這個問題有關。

查看Request.ItemParams.Item的MSDN頁面,我沒有看到任何區別。這兩個頁面都表示他們從Cookie,Form,QueryString或ServerVariables集合中獲取值。 (雖然他們列出的訂單有所不同。)

我見過this Stack Overflow post,但似乎主要集中在QueryString組件moreso比Request.Params.Item VS Request.Item的答案。

爲什麼我會用另一個呢?兩者之間有什麼區別嗎?

回答

5

這兩者在內容方面是嚴格等效的。而這裏的內容和它的搜索順序:

  1. 查詢字符串
  2. 形式
  3. 餅乾
  4. ServerVariables

至於用哪一個,那麼,在ASP.NET MVC應用程序它會更好地使用:

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

,因爲這考慮到路由和自定義值提供程序(例如JsonValueProvider)。但是,當然,一切都將取決於您的方案和具體要求。

+0

感謝您的明確答案。 – ckittel

相關問題