The answers presented before mine provide apt solutions to the problem, however, I feel that it is important to understand why this error results:
The Session
property of the Page
returns an instance of type HttpSessionState
relative to that particular request. Page.Session
is actually equivalent to calling Page.Context.Session
.
MSDN explains how this is possible:
Because ASP.NET pages contain a default reference to the System.Web namespace (which contains the
HttpContext
class), you can reference the members ofHttpContext
on an .aspx page without the fully qualified class reference toHttpContext
.
However, When you try to access this property within a class in App_Code, the property will not be available to you unless your class derives from the Page Class.
My solution to this oft-encountered scenario is that I never pass page objects to classes. I would rather extract the required objects from the page Session and pass them to the Class in the form of a name-value collection / Array / List, depending on the case.