C# – difference between HttpContext and HttpRequest

asp.netcnet

HttpRequest represents http client on server as per http://msdn.microsoft.com/en-us/library/system.web.httprequest.aspx it "Enables ASP.NET to read the HTTP values sent by a client during a Web request."

It was my understanding that HttpContext also does the same. As per MSDN http://msdn.microsoft.com/en-us/library/system.web.httpcontext.aspx it is "Encapsulates all HTTP-specific information about an individual HTTP request."

What we need to class for this ? How are they different and which should be used when ?

I am not able to figure out what is difference between them ? Can you please guide and help.

Thanks

Best Answer

HttpRequest is a subset of HttpContext. In other words, the HttpContext includes the response, the request, and various other data that's not relevant to a specific request or response; such as the web application, cached data, server settings and variables, session state, the authenticated user, etc.

For example:

HttpContext.Current.Request // This is the current HttpRequest object
HttpContext.Current.Response // This is the current HttpResponse object

I think if you dig around the APIs of each, you'll quickly understand how things are organized.