Common Code Class + Session + Request + Response

F

Filip

Hello,

(ASP.NET c#)
I want to put some common functions into one class and within this class I
need to have access to the Session, Request and Response. This class is not
page behind file!
I keep trying all sorts of namespaces, but to no avail. Keep getting:
"The name 'Session' does not exist in the class or namespace 'xyz xyz xyz'"
and "The type or namespace name 'Page' could not be found (are you missing a
using directive or an assembly reference?)"

Obviously I need to pass in information about the page where the function is
called from, but can't find a way of doing this.

Any help would be appreciated.
 
K

Ketchup Please

Use System.Web.httpContext.Current.

The code behind is in a class that inherits from the Page class that
provides you with the current HTTP context. Outside of such a page using
the property above allows you to get access to the current request context.

Example of use from some non page or non code-behind class file:
int currnetID =
Convert.ToInt32(System.Web.HttpContext.Current.Session["userID"])

EXPLANATION (cut and pasted from a response by the very helpful Kevin
Spencer from March 26 on the same topic):
The System.Web.UI.Page class is an HttpHandler; that is, it implements the
IHttpHandler Interface, because its purpose is to handle HTTP Requests. The
HttpHandler is given the entire context in which the HTTP Request occurs
(This is called the System.Web.HttpContext.Current object). The HttpContext
contains the Request, Response, Session, Application, and other elements of
the HttpContext of the current Request as Properties, in order to respond.
The HttpContext of the current Request is therefore, a member by default of
the Page class. In custom classes, you need to get a handle on the Current
HttpContext (which will only occur in the context of an HttpRequest) in
order to access these objects. This can be done by referring to the
System.Web.HttpContext.Current object. Once you've done that, you can refer
to the Application, for example, as
System.Web.HttpContext.Current.Application.

Good Luck
 
S

Saravana [MVP]

Hi ,

If you are creating this class separately from web project, then import
system.web in your project(if you dont have system.web reference, then add
that too). Then access your session variable using
HttpContext.Current.Session
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,278
Latest member
BuzzDefenderpro

Latest Threads

Top