Net also provides declarative application custom

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: of the week, months, product categories, SKUs, etc. Instead, set EnableViewState=false, cache that data on the server, and populate the control from the cache in Page_Load Can cache data or even HTML Use Control.Render() to obtain a control’s HTML 23 Outline IIS Web Applications Configuration Tracing State Management Caching Error Handling Error Handling .NET Common Language Runtime provides a unified exception architecture Runtime errors done using exceptions VB now supports try/catch/finally ASP.NET also provides declarative application custom error handling Automatically redirect users to error page when unhandled exceptions occur Prevents ugly error messages from being sent to users 24 Error Handling Custom Error Pages Can specify error pages for specific HTTP status codes in web.config <configuration> <customerrors mode=“remoteonly” defaultredirect=“error.htm”> <error statuscode=“404” redirect=“adminmessage.htm”/> <error statuscode=“403” redirect=“noaccessallowed.htm”/> </customerrors> </configuration> Error Handling Error Events Can override Page.HandleError to deal with any unhandled exception on that page Global application event raised if unhandled exception occurs Provides access to current Request Provides access to Exception object See HttpApplication.Error event 25 Error Handling Error Event What do you actually do when an error occurs? Use new EventLog class to write custom events to log when errors occur Use new SmtpMail class to send email to administrators Error Handling Writing to Event Log <%@ Import Namespace="System.Diagnostics" %> <%@ Assembly name="System.Diagnostics" %> <script language="C#" runat=server> public void Application_Error(object Sender, EventArgs E) { string LogName = "MyCustomAppLog"; string Message = "Url " + Request.Path + " Error: " + this.Error.ToString() // Create event log if it doesn’t exist if (! EventLog.SourceExists(LogName)) { EventLog.CreateEventSource(LogName, LogName); } // Fire off to event log EventLog Log = new EventLog(); Log.Source = LogName; Log.WriteEntry(Message, EventLogEntryType.Error); } </script> 26 Error Handling Sending SMTP Mail <%@ Import Namespace="System.Web.Util" %> <%@ Assembly name="System.Diagnostics" %> <script language="C#" runat=server> public void Application_Error(object Sender, EventArgs E) { MailMessage MyMessage = new MailMessage(); MyMessage.To = "[email protected]"; MyMessage.From = "MyAppServer"; MyMessage.Subject = "Unhandled Error!!!"; MyMessage.BodyFormat = MailFormat.Html; MyMessage.Body = "<html><body><h1>" + Request.Path + "</h1>" + Me.Error.ToString() + "</body></html>"; SmtpMail.Send(MyMessage); } </script> 27...
View Full Document

This note was uploaded on 04/04/2014 for the course CS 5950 taught by Professor Staff during the Summer '08 term at Western Michigan.

Ask a homework question - tutors are online