學習於:http://www.cnblogs.com/fish-li/archive/2011/12/06/2278463.html
有三個比較常見的用戶端資料來源:QueryString, Form, Cookie
可以在HttpRequest中訪問這三大對象,比如,可以從QueryString中擷取包含在URL中的一些參數, 可以從Form中擷取使用者輸入的表單資料, 可以從Cookie中擷取一些工作階段狀態以及其它的使用者個人化參數資訊。 除了這三大對象,HttpRequest還提供ServerVariables來讓我們擷取一些來自於Web伺服器變數。
一般情況下,如果我們在事先就能明確知道某個參數是來源於哪個集合,那麼直接存取那個集合,問題也就簡單了。 然而,更常見的資料來源通常只會是QueryString, Form,而且尤其是當在用戶端使用Jquery的$.ajax()這類技術時, 可以很隨意地將參數放到QueryString或者是Form中,那麼,服務端通常為了也能靈活地應對這一現況, 可以使用Request[]與Request.Params[] 這二種方式來訪問這些來自於使用者提交的資料。
這二個屬性都可以讓我們方便地根據一個KEY去【同時搜尋】QueryString、Form、Cookies 或 ServerVariables這4個集合。 通常如果請求是用GET方法發出的,那我們一般是訪問QueryString去擷取使用者的資料,如果請求是用POST方法提交的, 我們一般使用Form去訪問使用者提交的表單資料。而使用Params,Item可以讓我們在寫代碼時不必區分是GET還是POST。
這二個屬性唯一不同的是:
A.Item是依次訪問這4個集合,找到就返回結果,不會繼續訪問下去。
B.Params是在訪問時,先將4個集合的資料合併到一個新集合(集合不存在時建立), 然後再尋找指定的結果。
例子:如果一個URL中含有的參數key與頁面提交(submit)的input的name一樣的話,用Request["key"]只得到URL中參數的值,用Request.Params["key"]將會得到URL中參數值以及頁面中input的值(共兩個值)
頁面html:
<body> <p> Item結果:<%= this.ItemValue %></p> <p> Params結果:<%= this.ParamsValue %></p> <form id="form1" action="<%= Request.RawUrl %>" method="post"> <div> <input type="text" name="age" value="123" /> <input type="submit" value="提交" /> </div> </form></body>
後台:
public partial class RequestParamsTest : System.Web.UI.Page { protected string ItemValue; protected string ParamsValue; protected void Page_Load(object sender, EventArgs e) { String[] allKeys = Request.QueryString.AllKeys; if (allKeys.Length == 0) { Response.Redirect("~/RequestParamsTest.aspx?age=45", true); } ItemValue = Request["age"]; ParamsValue = Request.Params["age"]; } }
提交之前:Request["age"]的值為45,Request.Params["age"]的值也是45
提交之後 Request["age"]的值為45,Request.Params["age"]的值為【45,123】//把QueryString與Form中的資料合併到一個新的集合中了
小結:盡量不要使用Request.Params
Request[]的實現方式(原始碼):
//是一個預設的索引public string this[string key]{ get { string str = this.QueryString[key]; if (str != null) { return str; } str = this.Form[key]; if (str != null) { return str; } HttpCookie cookie = this.Cookies[key]; if (cookie != null) { return cookie.Value; } str = this.ServerVariables[key]; if (str != null) { return str; } return null; }}
//根據指定的key,依次訪問QueryString,Form,Cookies,ServerVariables這4個集合,如果在任意一個集合中找到了,就立即返回。
Request.Params[]實現方式(原始碼):
public NameValueCollection Params{ get { if (HttpRuntime.HasAspNetHostingPermission(AspNetHostingPermissionLevel.Low)) { return this.GetParams(); } return this.GetParamsWithDemand(); }}private NameValueCollection GetParams(){ if( this._params == null ) { this._params = new HttpValueCollection(0x40); this.FillInParamsCollection(); this._params.MakeReadOnly(); } return this._params;}private void FillInParamsCollection(){ this._params.Add(this.QueryString); this._params.Add(this.Form); this._params.Add(this.Cookies); this._params.Add(this.ServerVariables);}
//先判斷_params這個Field成員是否為null,如果是,則建立一個集合,並把QueryString,Form,Cookies,ServerVariables這4個集合的資料全部填充進來, 以後的查詢都直接在這個集合中進行。
//Params是NameValueCollection類型,NameValueCollection類在一個鍵下儲存多個字串值,剛好解釋了Request.Params["age"]的值為【45,123】
NameValueCollection類型中,【name】這個key對應一個ArrayList,所以它一個key可以存放多個值,且以逗號隔開
通過調用NameValueCollection類中的GetValues方法,可以得到一個串值,且不含逗號
string[] array = Request.Params.GetValues("name");
if( array != null )
foreach(string val in array)
其實不僅僅是Request.Params[]存在一個key下儲存多個值,只要類型是NameValueCollection的資料來源都存在這個問題,QueryString, Form,Param都有這樣的問題,只是Param需要合并4種資料來源。