Online Book Reader

Home Category

Programming Microsoft ASP.NET 4 - Dino Esposito [147]

By Root 5720 0
information that a malicious user might use against your system. Sensitive data includes user names, file system paths, connection strings, and password-related information. You can make error pages smart enough to determine whether the user is local or whether a custom header is defined, and to display more details that can be helpful to diagnose errors:

if (Request.UserHostAddress == "127.0.0.1") {

...

}

You can also use the Request.Headers collection to check for custom headers added only by a particular Web server machine. To add a custom header, you open the Properties dialog box of the application’s Internet Information Services (IIS) virtual folder and click the HTTP Headers tab.

Global Error Handling


A page Error handler catches only errors that occur within a particular page. This means that each page that requires error handling must point to a common piece of code or define its own handler. Such a fine-grained approach is not desirable when you want to share the same generic error handler for all the pages that make up the application. In this case, you can create a global error handler at the application level that catches all unhandled exceptions and routes them to the specified error page.

The implementation is nearly identical to page-level error handlers except that you will be handling the Error event on the HttpApplication object that represents your application. To do that, you write code in the predefined Application_Error stub of the application’s global.asax file:

void Application_Error(Object sender, EventArgs e)

{

...

}

You could do something useful in this event handler, such as sending an e-mail to the site administrator or writing to the Windows event log to say that the page failed to execute properly. ASP.NET provides a set of classes in the System.Net.Mail namespace for just this purpose.

void Application_Error(Object sender, EventArgs e)

{

// Code that runs when an unhandled error occurs

var exception = Server.GetLastError();

if (exception == null)

return;

var mail = new MailMessage { From = new MailAddress("automated@contoso.com") };

mail.To.Add(new MailAddress("administrator@contoso.com"));

mail.Subject = "Site Error at " + DateTime.Now;

mail.Body = "Error Description: " + exception.Message;

var server = new SmtpClient {Host = "your.smtp.server"};

server.Send(mail);

// Clear the error

Server.ClearError();

}

If the SMTP server requires authentication, you need to provide your credentials through the Credentials property of the SmtpClient class. Figure 7-3 shows the e-mail message being sent.

Figure 7-3. The e-mail message being sent when an error is handled globally.

As Figure 7-3 shows, the exception reported mentions a generic HTTP unhandled exception. Note that GetLastError returns the real exception in the context of Page_Error, but not later in the context of Application_Error. In the application context, the exception caught is a generic HTTP exception that wraps the original exception internally. To retrieve the real exception, you must go through the InnerException property, as shown here:

void Application_Error(Object sender, EventArgs e)

{

// This is a generic HTTP failure exception

var exception = Server.GetLastError();

if (exception == null)

return;

// Put your hands on the original exception

var originalException = exception.InnerException;

...

}

Essentially, when ASP.NET detects an internal application error—like it is an exception being thrown by one of the pages—it configures itself for an HTTP 500 response. The ASP.NET error-handling mechanism captures HTTP 500 errors but not other HTTP errors, such as 404. Errors other than HTTP 500 are handled by the Web server, and all that you can do is configure the ASP.NET error-handling machinery (and to some extent the routing mechanism too) to redirect automatically where you like. No full control over 404 and other HTTP errors is possible in ASP.NET Web Forms.

Note

What takes precedence if you have an application-level error handler and a page-level handler? The page

Return Main Page Previous Page Next Page

®Online Book Reader