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>