DekGenius.com
[ Team LiB ] Previous Section Next Section

14.2 Properties Reference

Application

HttpApplicationState = Context.Application

Returns the current Application state object.

Parameter

HttpApplicationState

An HttpApplicationState object that will receive the value of the property.

Example

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

Notes

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.

ApplicationInstance

Context.ApplicationInstance = HttpApplication
HttpApplication = Context.ApplicationInstance

Returns or sets the current HttpApplication object.

Parameter

HttpApplication

An HttpApplication object that will receive or set the value of the ApplicationInstance property.

Notes

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.

Cache

HttpCache = Context.Cachea

Returns an instance of the Cache class.

Parameter

HttpCache

An Object variable of type Cache.

Example

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

Notes

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.

Current

HttpContext = HttpContext.Current

Retrieves the current HttpContext instance.

Parameter

HttpContext

The current context.

Example

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

Notes

Current is a shared (static) property, indicating that you can access it without creating an instance of the HttpContext class.

Error

Exception = Context.Error

Returns the first error, if any, associated with the current request.

Parameter

Exception

An Exception variable to receive the value of the property.

Example

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
Handler

IHttpHandler = Context.Handler
Context.Handler = IHttpHandler

Sets or returns an instance of the IHttpHandler for the current request.

Parameter

IHttpHandler

An Object variable of a type that implements the IHttpHandler interface.

Notes

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.

IsCustomErrorEnabled

Boolean = Context.IsCustomErrorEnabled

Returns a Boolean value specifying whether custom errors are enabled for the current request.

Parameter

Boolean

A Boolean variable to receive the value of this property.

Example

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

Notes

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.

IsDebuggingEnabled

Boolean = Context.IsDebuggingEnabled

Returns a Boolean value specifying whether debugging is enabled for the current request.

Parameter

Boolean

A Boolean variable to receive value of this flag.

Example

The example displays True if debugging is enabled; otherwise, it displays False:

Sub Page_Load( )
   Message.Text = "Debugging Enabled?" & _
      Context.IsDebuggingEnabled
End Sub

Notes

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.

Request

HttpRequest = Context.Request

Returns the HttpRequest object for the current request.

Parameter

HttpRequest

An HttpRequest variable to receive the current HttpRequest object.

Notes

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.

Response

HttpResponse = Context.Response

Returns the HttpResponse object for the current request.

Parameter

HttpResponse

An HttpResponse variable to receive the current HttpResponse object.

Notes

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.

Session

HttpSessionState = Context.Session

Returns the HttpSession object for the current request.

Parameters

HttpSessionState

An HttpSessionState object to receive the current session.

Notes

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.

SkipAuthorization

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.

Parameter

Boolean

A Boolean variable returning or setting the flag regarding authorization checks.

Example

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

Notes

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

timestamp = Context.Timestamp

Returns a DateTime object containing the date and time of the request on the server.

Parameter

timestamp

An Object variable of type DateTime.

Example

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
Trace

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.

Parameter

TraceContext

A TraceContext variable to receive the TraceContext object for the current request.

Example

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

Notes

Trace can be enabled by setting the Trace attribute of the @ Page directive to True.

User

IPrincipal = Context.User
Context.User = IPrincipal

Returns or sets the IPrincipal object for the current request.

Parameter

IPrincipal

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:

Identity

A property that returns a class that implements the IIdentity interface.

IsInRole(Role as String)

A method that returns a Boolean indicating whether the current principal belongs to the specified Role.

The IIdentity interface provides several useful properties:

AuthenticationType

A property that returns the type of authentication used, if the request is authenticated.

IsAuthenticated

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.

Name

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.

Example

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

Notes

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.

    [ Team LiB ] Previous Section Next Section