Is there any difference between Request("key") and Request.Params("key")?
In an ASP.NET MVC3 application I have a function that looks like:
Public Sub DoSomething(controllerCtx As Controller开发者_高级运维Context)
....
' Which to use? and Why?
Dim val = controllerCtx.HttpContext.Request.Params.Item("someKey")
Dim val = controllerCtx.HttpContext.Request.Item("someKey")
....
End Sub
(I'm aware that Item
is a Default
property in both and can be removed, that's not relevant in this question.)
Looking at the MSDN pages for Request.Item
and Params.Item
, I'm not seeing any differences. Both pages say they get values from: Cookies, Form, QueryString, or ServerVariables collections. (though they do list the orders differently.)
I've seen this Stack Overflow post, but it seems the answers focused on the QueryString
component moreso than the Request.Params.Item
vs Request.Item
.
Why would would I use one over the other? Is there any difference between the two at all?
The two are strictly equivalent in terms of contents. And here's the contents and the order in which it is searched:
- QueryString
- Form
- Cookies
- ServerVariables
As far as which one to use, well, in an ASP.NET MVC application it would be better to use:
controllerCtx.Controller.ValueProvider.GetValue("someKey");
as this take into account routes and custom value providers (such as for example the JsonValueProvider) as well. But of course everything would depend on your scenario and specific requirements.
精彩评论