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

发布于 2024-12-02 10:56:59 字数 1143 浏览 1 评论 0 原文

在 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 集合。 (尽管他们确实列出了不同的订单。)

我见过这篇 Stack Overflow 帖子,但似乎答案更多地集中在 QueryString 组件上,而不是 Request.Params.Item vs 请求.Item

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

In an ASP.NET MVC3 application I have a function that looks like:

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

(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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

北方。的韩爷 2024-12-09 10:56:59

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

  1. QueryString
  2. Form
  3. Cookies
  4. ServerVariables

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

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

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

The two are strictly equivalent in terms of contents. And here's the contents and the order in which it is searched:

  1. QueryString
  2. Form
  3. Cookies
  4. 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文