Re: Common Code Class + Session + Request + Response

From: Ketchup Please (GoodStuff_at_Heinz57.com)
Date: 05/21/04


Date: Thu, 20 May 2004 22:42:12 -0700

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

"Filip" <filipk69@hotmail.com> wrote in message
news:uxjPBguPEHA.556@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.
>
>
>



Relevant Pages

  • Re: Web.HttpContext.Current.User.Identity.Name is blank
    ... And it is put to the Context on every request i.e user is athenticated with ... forms auth and then in Application_AuthenticateRequest you populate the ... I am asking because HttpContext is recreated ... for every request which means that user details must also. ...
    (microsoft.public.dotnet.framework.aspnet.security)
  • Re: Web.HttpContext.Current.User.Identity.Name is blank
    ... Yes it is put in the Context on every request in Global.asax.vb ... > forms auth and then in Application_AuthenticateRequest you populate the ... I am asking because HttpContext is recreated ...
    (microsoft.public.dotnet.framework.aspnet.security)
  • Re: Class Capability Differences
    ... The HttpHandler is given the entire context in which the HTTP ... > other elements of the HttpContext of the current Request as Properties, ...
    (microsoft.public.dotnet.framework.aspnet)
  • RE: OWA works from client but not from server
    ... Recently, the SBS domain users cannot access OWA from the SBS server, ... the error message may be one of the ... HTTP 400 - Bad Request ...
    (microsoft.public.windows.server.sbs)
  • Re: OWA works from client but not from server
    ... Recently, the SBS domain users cannot access OWA from the SBS server,   ... the error message may be one of the ... HTTP 400 - Bad Request ...
    (microsoft.public.windows.server.sbs)