Saturday, June 19, 2010

Request.Params == QueryString + Form + ServerVariables + Cookies

I'm doing some work with the ASP.NET Request object and have just discovered that I can get all of the "params" from one property on the Request object.

If I'm not already in a context where the Request object is a member of that context (such as an MVC Controller or a codebehind page) then I will usually access the Request object through the HttpContext.Current object. I always wrap this access in a double are-you-null before trying to access it:

if (HttpContext.Current != null && HttpContext.Current.Request != null)
{
}

These four objects:

HttpContext.Current.Request.QueryString
HttpContext.Current.Request.Form
HttpContext.Current.Request.ServerVariables
HttpContext.Current.Request.Cookies

Can be accessed through a single collection if you reference the:

HttpContext.Current.Request.Params

which gets a combined collection of the other four.

 

No comments:

Post a Comment