Request[]与Request.Params[] 差别
Request[]与Request.Params[] ,这二个属性都可以让我们方便地根据一个KEY去【同时搜索】QueryString、Form、Cookies 或 ServerVariables这4个集合
这二个属性唯一不同的是:Item是依次访问这4个集合,找到就返回结果,而Params是在访问时,先将4个集合的数据合并到一个新集合(集合不存在时创建), 然后再查找指定的结果。
Request[]实现原理(.net源代码)
// System.Web.HttpRequest /// <summary>Gets the specified object from the <see cref="P:System.Web.HttpRequest.QueryString" />, <see cref="P:System.Web.HttpRequest.Form" />, <see cref="P:System.Web.HttpRequest.Cookies" />, or <see cref="P:System.Web.HttpRequest.ServerVariables" /> collections.</summary> /// <returns>The <see cref="P:System.Web.HttpRequest.QueryString" />, <see cref="P:System.Web.HttpRequest.Form" />, <see cref="P:System.Web.HttpRequest.Cookies" />, or <see cref="P:System.Web.HttpRequest.ServerVariables" /> collection member specified in the <paramref name="key" /> parameter. If the specified <paramref name="key" /> is not found, then null is returned.</returns> /// <param name="key">The name of the collection member to get. </param> public string this[string key] { get { string text = this.QueryString[key]; if (text != null) { return text; } text = this.Form[key]; if (text != null) { return text; } HttpCookie httpCookie = this.Cookies[key]; if (httpCookie != null) { return httpCookie.Value; } text = this.ServerVariables[key]; if (text != null) { return text; } return null; } }
Request.Params[]源码
// System.Web.HttpRequest /// <summary>Gets a combined collection of <see cref="P:System.Web.HttpRequest.QueryString" />, <see cref="P:System.Web.HttpRequest.Form" />, <see cref="P:System.Web.HttpRequest.Cookies" />, and <see cref="P:System.Web.HttpRequest.ServerVariables" /> items.</summary> /// <returns>A <see cref="T:System.Collections.Specialized.NameValueCollection" /> object. </returns> public NameValueCollection Params { get { if (HttpRuntime.HasAspNetHostingPermission(AspNetHostingPermissionLevel.Low)) { return this.GetParams(); } return this.GetParamsWithDemand(); } }
// System.Web.HttpRequest private NameValueCollection GetParams() { if (this._params == null) { this._params = new HttpValueCollection(64); this.FillInParamsCollection(); this._params.MakeReadOnly(); } return this._params; }
// System.Web.HttpRequest private void FillInParamsCollection() { this._params.Add(this.QueryString); this._params.Add(this.Form); this._params.Add(this.Cookies); this._params.Add(this.ServerVariables); }
参考网页http://www.cnblogs.com/fish-li/archive/2011/12/06/2278463.html