【问题标题】:Difference between Request.Form and Request.QueryString?Request.Form 和 Request.QueryString 的区别?
【发布时间】:2013-12-20 06:01:19
【问题描述】:

有人能告诉我Request.FormRequest.QueryString 之间的确切区别吗?

我知道一个区别,比如

如果HTTP请求方式为POST,则用户提交的数据在 Request.Form() 集合

如果HTTP请求方法是GET,那么用户提交的数据在 Request.QueryString() 集合

还有什么不同吗?任何例子都将不胜感激。

【问题讨论】:

    标签: c# asp.net http request.querystring request.form


    【解决方案1】:

    Request.Form 中,数据发布在http 请求正文中,而在QueryString 中,数据通过url 发送。

    【讨论】:

      【解决方案2】:

      我发现了一些其他的不同

      Request("id") 
      

      将首先尝试定位

      Request.Form("id") 
      

      然后

      Request.Querystring("id") 
      

      然后

      Request.Cookies("id") 
      

      最后

      Request.ServerVariables("id") 
      

      如果可能,建议使用显式命名约定,因为它更高效且更具可读性。它还使您能够确定信息的来源,因为系统将在第一次命中后停止... . 如果指定信息的位置,系统也更快。

      我们可以参考此链接了解更多详情:

      http://www.hanselman.com/blog/ASPNETParamsCollectionVsQueryStringFormsVsRequestindexAndDoubleDecoding.aspx

      但是任何人都知道其他区别,我真的很感激。

      【讨论】:

        【解决方案3】:

        如 MSDN 所述,

        (Request.Form):Request.Form(element)的值是一个数组 请求正文中出现的元素的值。你可以 通过调用确定参数值的数量 Request.Form(元素).Count。如果一个参数没有多个 与之关联的值,计数为 1。如果参数不是 找到,计数为0。

        and (Request.QueryString):Request.QueryString(parameter)的值 是出现在中的所有参数值的数组 请求参数。您可以通过以下方式确定参数值的数量 调用 Request.QueryString(parameter).Count。如果变量不 有多个与之关联的数据集,计数为 1。如果 变量未找到,计数为 0。

        所以,有几点需要注意:

        在一个典型的页面表单中,我们可能会包含一些隐藏元素:

        <form method="post">
           <input type="hidden" name="lol" value="cat" />
           <input type="text" />
        </form>
        

        隐藏元素(如果没记错的话)不会显示在QueryString 中。所以,我会假设Request.QueryString 中没有显示一些东西。不幸的是,我正在新机器上重新安装开发应用程序,目前无法对此进行测试,但如果我是对的,当您POST 表单时,将发送有关表单及其内容的更多详细信息。当您访问QueryString 时,您只会看到构成整个 URL 的内容,例如:

        http://somesite.com/index.html?v=1&NonHiddenElement=lol&ManualValue=hello

        【讨论】:

        • 好!真的很感谢你,在这里我有更多的不同;)
        • 谢谢,如果需要更多解释,请告诉我,我会尽力提供帮助。当我再次安装我的开发应用程序时,我会确保更新更多细节。 :)
        【解决方案4】:

        Request.Form - 表示您要检索已发布表单的值。

        Request.QueryString - 表示您想要检索已通过查询字符串传递的值。

        【讨论】:

          【解决方案5】:

          Request.Form()

          • Form 集合检索发布到 HTTP 请求正文的表单元素的值,仅检索您的表单中存在的那些元素和值。

          Request.QueryString()

          • QueryString 集合检索 HTTP 查询字符串中的变量值,您可以在此处附加任何自定义变量和值,这些事件在您的表单中不存在。

          【讨论】:

            【解决方案6】:

            Request.Form 集合

            The Form collection retrieves the values of form elements posted to the HTTP request body, with a form using the POST method.
            Form input is contained in headers. It is wise to not trust the data that is contained in headers, as this information can be falsified by malicious users. For example, do not rely on data such as cookies to securely identify a user.
            As a security precaution, always encode header data or user input before using it. A general method of encoding data is to use Server.HTMLEncode. Alternatively, you can validate header data and user input with a short function such as the one described in Validating User Input to Avoid Attacks. For more detailed information about developing secure Web applications, see chapter 12 of MS Press - Writing Secure Code.
            Syntax
            Request.Form(element)[(index)|.Count]
            Parameters
            element
            The name of the form element from which the collection is to retrieve values.
            index
            An optional parameter that enables you to access one of multiple values for a parameter. It can be any integer in the range 1 to Request.Form(parameter).Count.
            Applies To
            Request Object
            Remarks
            The Form collection is indexed by the names of the parameters in the request body. The value of Request.Form(element) is an array of all the values of element that occur in the request body. You can determine the number of values of a parameter by calling Request.Form(element).Count. If a parameter does not have multiple values associated with it, the count is 1. If the parameter is not found, the count is 0.
            To reference a single value of a form element that has multiple values, you must specify a value for the index parameter. The index parameter may be any number between 1 and Request.Form(element).Count. If you reference one of multiple form parameters without specifying a value for index, the data is returned as a comma-delimited string.
            When you use parameters with Request.Form, the Web server parses the HTTP request body and returns the specified data. If your application requires unparsed data from the form, you can access it by calling Request.Form without any parameters.
            

            Request.QueryString 集合

            The QueryString collection retrieves the values of the variables in the HTTP query string. The HTTP query string is specified by the values following the question mark (?). Several different processes can generate a query string. For example, the following anchor tag generates a variable named string with the value "this is a sample."
            <A HREF="example.asp?string=this is a sample">string sample</A>
            
            Query strings are also generated by sending a form or by a user typing a query into the address box of the browser.
            Query strings are contained in request headers. It is wise to not trust the data that is contained in headers, as this information can be falsified by malicious users. For example, do not rely on data such as cookies to securely identify a user.
            As a security precaution, always encode header data or user input before using it. A general method of encoding data is to use Server.HTMLEncode. Alternatively, you can validate header data and user input with a short function such as the one described in Validating User Input to Avoid Attacks. For more detailed information about developing secure Web applications, see chapter 12 of MS Press - Writing Secure Code.
            Syntax
            Request.QueryString(variable)[(index)|.Count]
            Parameters
            variable
            Specifies the name of the variable in the HTTP query string to retrieve.
            index
            An optional parameter that enables you to retrieve one of multiple values for variable. It can be any integer value in the range 1 to Request.QueryString(variable).Count.
            Applies To
            Request Object
            Remarks
            The QueryString collection is a parsed version of the QUERY_STRING variable in the ServerVariables collection. It enables you to retrieve the QUERY_STRING variable by name. The value of Request.QueryString(parameter) is an array of all of the values of parameter that occur in QUERY_STRING. You can determine the number of values of a parameter by calling Request.QueryString(parameter).Count. If a variable does not have multiple data sets associated with it, the count is 1. If the variable is not found, the count is 0.
            To reference a QueryString variable in one of multiple data sets, you specify a value for index. The index parameter can be any value between 1 and Request.QueryString(variable).Count. If you reference one of multiple QueryString variables without specifying a value for index, the data is returned as a comma-delimited string.
            When you use parameters with Request.QueryString, the server parses the parameters sent to the request and returns the specified data. If your application requires unparsed QueryString data, you can retrieve it by calling Request.QueryString without any parameters.
            You can use an iterator to loop through all the data values in a query string.
            

            例如,如果发送以下请求:

            for more details click this link

            【讨论】:

            • 如果您从某处复制,请引用您的来源。
            猜你喜欢
            • 1970-01-01
            • 2011-01-14
            • 2013-05-18
            • 2010-10-08
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多