470,632 Members | 2,300 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,632 developers. It's quick & easy.

Common Code Class + Session + Request + Response

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.

Nov 18 '05 #1
5 2443
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.Ses sion["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

"Filip" <fi******@hotmail.com> wrote in message
news:ux*************@TK2MSFTNGP10.phx.gbl...
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.

Nov 18 '05 #2
Hi,

you should use HttpContext.Current.XXX

XXX = Request, rESPONSE, Session ...

HTH

Natty Gur[MVP]

blog : http://weblogs.asp.net/ngur
Mobile: +972-(0)58-888377
*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 18 '05 #3
Thanks Ketchup
Nov 18 '05 #4
Thanks Natty Gur
Nov 18 '05 #5
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
--
Saravana
Microsoft MVP - ASP.NET
www.extremeexperts.com

"Filip" <fi******@hotmail.com> wrote in message
news:ux*************@TK2MSFTNGP10.phx.gbl...
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.

Nov 18 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by collie | last post: by
1 post views Thread by Nazim | last post: by
reply views Thread by TRB_NV | last post: by
1 post views Thread by Korara | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.