[ Team LiB ] |
14.2 Properties Reference
Returns the current Application state object. Parameter
ExampleThe 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 NotesOften, 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.
Returns or sets the current HttpApplication object. Parameter
NotesGenerally, 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.
Returns an instance of the Cache class. Parameter
ExampleThe 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 NotesNote 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.
Retrieves the current HttpContext instance. Parameter
ExampleThis 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 NotesCurrent is a shared (static) property, indicating that you can access it without creating an instance of the HttpContext class.
Returns the first error, if any, associated with the current request. Parameter
ExampleThe 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
Sets or returns an instance of the IHttpHandler for the current request. Parameter
NotesThe 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.
Returns a Boolean value specifying whether custom errors are enabled for the current request. Parameter
ExampleThe 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 NotesThis 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.
Returns a Boolean value specifying whether debugging is enabled for the current request. Parameter
ExampleThe example displays True if debugging is enabled; otherwise, it displays False: Sub Page_Load( ) Message.Text = "Debugging Enabled?" & _ Context.IsDebuggingEnabled End Sub NotesThis 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.
Returns the HttpRequest object for the current request. Parameter
NotesThis 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.
Returns the HttpResponse object for the current request. Parameter
NotesThis 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.
Returns the HttpSession object for the current request. Parameters
NotesThis 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.
Sets or returns a flag indicating whether the URLAuthorization module will skip the authorization check. The default is False. Parameter
ExampleThe 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 NotesTo 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.
Returns a DateTime object containing the date and time of the request on the server. Parameter
ExampleThe 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
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
ExampleThe 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 NotesTrace can be enabled by setting the Trace attribute of the @ Page directive to True.
Returns or sets the IPrincipal object for the current request. Parameter
ExampleThe 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 NotesUnderstanding 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 ] |