Posts Tagged ‘Page’

The ASP.NET Context Object (HTTPContext class)

Tuesday, March 10th, 2009

Usually when you are using ASP.NET, much of the code that you write will be in a page. Since the Page class has properties to access Request, Response and other useful objects, you normally have access to whatever you need. However, sometimes, you aren’t working in a Page (eg. you are coding in App_Code or in a DLL), and it is annoying to have to pass these objects as parameters to every function that may need them.

ASP.NET has a solution to this problem – the HttpContext class. Thiosd class has a shared (static for C# coders) property called “Current”, which can be used to access the current context object. This object contains most of all of the objects which you might need in properties. For example, you can call HttpContext.Current.Response.Redirect.