Request,Request.QueryString,Request.Form

來源:互聯網
上載者:User

Request.QueryString:擷取 HTTP 查詢字串變數集合。一般是擷取Get方式提交的資料,包含由用戶端發送的查詢字串變數的集合。

例如,如果請求URL 為 http://www.cnblogs.com/errorif/posts.aspx?id=44,則 System.Web.HttpRequest.QueryString 的值為“id=44”。

 

Request.Form:擷取表單變數集合。一般是擷取以Form形式Post提交的資料

 

Request:從幾個集合取資料是有順序的,從前到後的順序依次是 QueryString,Form,Cookie最後是ServerVariables。Request對象按照這樣的順序依次搜尋這幾個集合中的變數,如果有符合的就中止。

其實現代碼為:

 1 public string this[string key]
 2 {
 3     get
 4     {
 5         string str = this.QueryString[key];
 6         if (str != null)
 7         {
 8             return str;
 9         }
10         str = this.Form[key];
11         if (str != null)
12         {
13             return str;
14         }
15         HttpCookie cookie = this.Cookies[key];
16         if (cookie != null)
17         {
18             return cookie.Value;
19         }
20         str = this.ServerVariables[key];
21         if (str != null)
22         {
23             return str;
24         }
25         return null;
26     }
27 }
28  

 

我們來分析下.
假設有個頁面 test.aspx?id=111
這裡我們的頁面是用GET的方法.這時用Request.QueryString("id")與Request("id")是一樣得,應該如果不指定REQUEST得集合,首先就會從QueryString搜尋.

而如果我們的頁面是用的是POST的方法發送資料給test.asp,那麼用Request.QueryString("id")是不行的了(他只能取GET),而要用Request.From("id"),而如果還用Request("id")他也能取到資料,但先檢測QuerySstring的值,顯然速度就慢了.

 

 

下面是個檢測的例子:

 

<% 
If Request("submit")<>"" then 
    Response.Write "直接取:"& Request("username") & "<br>" 
    Response.Write "取Get:" & Request.QueryString("username") & "<br>" 
    Response.Write "取Post:" & Request.Form("username") & "<br>" 
End if 
%> 
<form name=form1 action="" method=post> 
<input type=test name="username" value="postuser"> 
<input type=submit name="submit" value="test"> 
</form>

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.