HttpApplicationState = Context.Application
Returns the current Application state object.
An HttpApplicationState object that will receive the value of the property.
The example sets an application value using the Application instance exposed by the Page class and gets the instance from Context. Finally, it displays the newly set value by using the HttpApplicationState instance retrieved from the Context object (proving they are the same object):
Sub Page_Load( ) Dim App as HttpApplicationState Page.Application("Test")="Value" App = Context.Application Message.Text = App("Test") End Sub
Often, you will use the copy of Application from the Page class. However, for objects not derived from Page (for instance, HttpHandlers and HttpModules) this is a convenient way to access the Application state.
Do not confuse this property, which returns an instance of type HttpApplicationState, with the ApplicationInstance property, which returns an instance of type HttpApplication.
Context.ApplicationInstance = HttpApplication HttpApplication = Context.ApplicationInstance
Returns or sets the current HttpApplication object.
An HttpApplication object that will receive or set the value of the ApplicationInstance property.
Generally, you do not need to use the HttpApplication object, since the properties it exposes are usually available through other objects. One exception is when accessing all the methods that allow you to add event handlers for this request.
HttpCache = Context.Cachea
Returns an instance of the Cache class.
An Object variable of type Cache.
The example retrieves an instance of the Cache class into a local variable and then adds a value to the cache:
Sub Application_BeginRequest( ) Dim myCache As Cache myCache = Context.Cache myCache.Add("Test", "Test", Nothing, _ System.DateTime.Now.AddHours(1), Nothing, _ CacheItemPriority.High, Nothing) End Sub
Note that rather than using the Page_Load event, the example above shows the Application_BeginRequest event handler in global.asax; a common use of the Cache property is to access the cache at points during request processing when the Cache property of the Page object is not available, such as before the Page object is instantiated.
HttpContext = HttpContext.Current
Retrieves the current HttpContext instance.
The current context.
This example uses the IsDebuggingEnabled property, described later in this chapter, to show whether debugging is enabled:
Sub Page_Load( ) Message.Text = HttpContext.Current.IsDebuggingEnabled.ToString( ) End Sub
Current is a shared (static) property, indicating that you can access it without creating an instance of the HttpContext class.
Exception = Context.Error
Returns the first error, if any, associated with the current request.
An Exception variable to receive the value of the property.
The example checks to see if the Error property on the current context object is null (Nothing in VB.NET). If it is not null, it displays the error; otherwise, it displays a message indicating there is no error.
Sub Page_Load( ) If Not HttpContext.Current.Error Is Nothing then Message.Text = HttpContext.Current.Error.ToString( ) Else Message.Text = "No error detected" End If End Sub
IHttpHandler = Context.Handler Context.Handler = IHttpHandler
Sets or returns an instance of the IHttpHandler for the current request.
An Object variable of a type that implements the IHttpHandler interface.
The Handler property can be used to specify special handling for this request. To understand this idea, you need to understand how requests are processed in IIS. When a request comes in, unless the item requested is a simple HTML file, the request is generally handled by an Internet Server API (ISAPI) extension. For instance, classic ASP and ASP.NET pages are directed to ISAPI applications that process the request. ISAPI applications are reasonably easy to write in principal, but are very difficult to create in practice. The IHttpHandler interface is the .NET way of allowing the developer to write code to for requests to be handled in a way similar to ISAPI applications in IIS, without all of the problems of ISAPI.
Boolean = Context.IsCustomErrorEnabled
Returns a Boolean value specifying whether custom errors are enabled for the current request.
A Boolean variable to receive the value of this property.
The following example displays True if custom errors are enabled; otherwise, it displays False.
Sub Page_Load( ) Message.Text = "Custom Error Enabled?" & _ Context.IsCustomErrorEnabled End Sub
This flag is controlled by the customErrors section in web.config. If the customErrors element's mode attribute is set to On, IsCustomErrorEnabled returns True. If the customErrors element's mode attribute is set to False or RemoteOnly, this flag is False.
Boolean = Context.IsDebuggingEnabled
Returns a Boolean value specifying whether debugging is enabled for the current request.
A Boolean variable to receive value of this flag.
The example displays True if debugging is enabled; otherwise, it displays False:
Sub Page_Load( ) Message.Text = "Debugging Enabled?" & _ Context.IsDebuggingEnabled End Sub
This flag is controlled by the compilation section in web.config. If the compilation section's debug attribute is set to True, IsDebuggingEnabled returns True. If the debug attribute in the compilation section is set to False, this property is False.
HttpRequest = Context.Request
Returns the HttpRequest object for the current request.
An HttpRequest variable to receive the current HttpRequest object.
This property is provided for applications other than ASP.NET pages (where the Page.Request property is normally used to retrieve the HttpRequest object). Code that does not have access to the properties of the Page class includes HttpHandlers and HttpModules, as well as event handlers in global.asax.
HttpResponse = Context.Response
Returns the HttpResponse object for the current request.
An HttpResponse variable to receive the current HttpResponse object.
This property is provided for applications other than ASP.NET pages (where the Page.Response property is normally used to retrieve the HttpResponse object). Code that does not have access to the properties of the Page class includes HttpHandlers and HttpModules, as well as global.asax. One common reason for using Context.Response is to write cookies in an HttpModule.
HttpSessionState = Context.Session
Returns the HttpSession object for the current request.
An HttpSessionState object to receive the current session.
This property is provided for applications other than ASP.NET pages (where the Page.Session property is normally used to retrieve the HttpSessionState object). Code that does not have access to the properties of the Page class includes HttpHandlers and HttpModules, as well as global.asax. When using Context.Session in the Application event handlers of global.asax, Session is not available in the Application BeginRequest event, but can be used in, for instance, the Application_PreRequestHandlerExecute event.
Boolean = Context.SkipAuthorization Context.SkipAuthentication = Boolean
Sets or returns a flag indicating whether the URLAuthorization module will skip the authorization check. The default is False.
A Boolean variable returning or setting the flag regarding authorization checks.
The following example retrieves the status of the SkipAuthorization property and displays it in the Message label control:
Sub Page_Load( ) Message.Text = "SkipAuthorization? " _ & Context.SkipAuthorization End Sub
To set this value, the ControlPrincipal Flag must be set in the Flags property of the SecurityPermission object. This property is used internally by the Forms and Passport authentication modules.
timestamp = Context.Timestamp
Returns a DateTime object containing the date and time of the request on the server.
An Object variable of type DateTime.
The following example retrieves the date and time of the request and displays it in the Message label control:
Sub Page_Load( ) Message.Text = "Date/Time of Request: " _ & Context.Timestamp End Sub
TraceContext = Context.Trace
Returns the TraceContext for the current request. The members of the TraceContext class are listed in the entry for the Trace property in Chapter 12.
A TraceContext variable to receive the TraceContext object for the current request.
The example retrieves the date and time of the request and displays it in the Message label control.
Sub Page_Load( ) Message.Text = "Trace Enabled? " _ & Context.Trace.IsEnabled End Sub
Trace can be enabled by setting the Trace attribute of the @ Page directive to True.
IPrincipal = Context.User Context.User = IPrincipal
Returns or sets the IPrincipal object for the current request.
An object that implements the IPrincipal interface. The IPrincipal interface is implemented by the GenericPrincipal and the WindowsPrincipal classes. IPrincipal defines one property and one method:
A property that returns a class that implements the IIdentity interface.
A method that returns a Boolean indicating whether the current principal belongs to the specified Role.
The IIdentity interface provides several useful properties:
A property that returns the type of authentication used, if the request is authenticated.
A property that returns a Boolean indicating if the user has been authenticated. This property should be checked (to ensure it is True) before checking for AuthenticationType or Name. IsAuthenticated returns False if anonymous authentication is enabled in Internet Information Server and neither Forms nor another authentication method is in use by ASP.NET.
A property that returns the Name of the current user. The user name format depends upon the type of authentication used. For Windows authentication, the name is in the format of DOMAIN\UserName.
The example checks to see if the user is authenticated and if so, returns the name of the user:
Sub Page_Load( ) Message.Text = "User.Identity.IsAuthenticated? " _ & Context.User.Identity.IsAuthenticated & "<br/>" If Context.User.Identity.IsAuthenticated Then Message.Text = Message.Text & "User.Identity.Name? " _ & Context.User.Identity.Name & "<br/>" End If End Sub
Understanding the values you receive back when checking the User property requires an understanding of ASP.NET and Internet Information Server, since the returned values depend on settings in both.