03.11.2016 Views

Beginning ASP.NET 4.5 in CSharp and VB Opsylum

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Exception H<strong>and</strong>l<strong>in</strong>g x 691<br />

‰ On—Every visitor to your site always sees the custom error page when an error occurs.<br />

‰ Off—The custom error page is never shown <strong>and</strong> full error details are displayed on the page.<br />

‰ RemoteOnly—The full error details are shown to local users (brows<strong>in</strong>g the site from the same<br />

mach<strong>in</strong>e as the site runs on), while all other users get to see the custom error page. This sett<strong>in</strong>g<br />

enables you to see error messages on your site dur<strong>in</strong>g development, while your users are<br />

always presented with the custom error page.<br />

With<strong>in</strong> the open<strong>in</strong>g <strong>and</strong> clos<strong>in</strong>g tags of the customErrors element you def<strong>in</strong>e separate <br />

elements, one for each HTTP status code you want to support. The previous configuration def<strong>in</strong>es<br />

two custom pages: one that is shown when the requested page could not be found (a 404 status<br />

code) <strong>and</strong> one that is shown when a server error occurs (a 500 code).<br />

For all other HTTP status codes you haven’t def<strong>in</strong>ed explicitly, the defaultRedirect attribute is<br />

used to determ<strong>in</strong>e the custom error page.<br />

The redirectMode attribute determ<strong>in</strong>es the way the new page is shown to the user <strong>and</strong> is discussed<br />

later <strong>in</strong> this chapter.<br />

Although custom error pages shield your users from the exception details, they don’t help <strong>in</strong> <strong>in</strong>form<strong>in</strong>g<br />

you that an exception occurred. All these pages do is hide the real error <strong>and</strong> show a page with a<br />

custom error message <strong>in</strong>stead. To be notified about these exceptions, you could write some code that<br />

looks at the exception <strong>and</strong> then sends you an e-mail with the details. Alternatively, you could write<br />

code that <strong>in</strong>serts the error details <strong>in</strong> a database, or writes them to a text file.<br />

<strong>ASP</strong>.<strong>NET</strong> offers you a h<strong>and</strong>y, central location to write code that is triggered when an exception<br />

occurs. You write this code <strong>in</strong> a special event h<strong>and</strong>ler called Application_Error <strong>in</strong>side the Global.<br />

asax file that you saw first <strong>in</strong> Chapter 11. Inside this event h<strong>and</strong>ler, you can collect relevant data<br />

about the exception, stick it <strong>in</strong> an e-mail message, <strong>and</strong> send it to your own Inbox. This gives you<br />

detailed <strong>in</strong>formation about exceptions that occur on your site, aid<strong>in</strong>g <strong>in</strong> fix<strong>in</strong>g the problem as soon<br />

as possible. You see how to write this code <strong>in</strong> the next exercise.<br />

TRY IT OUT<br />

H<strong>and</strong>l<strong>in</strong>g Exceptions Site Wide<br />

In this Try It Out you learn how to write code <strong>in</strong> the Global.asax file to send the exception message by<br />

e-mail. Additionally, you see how to create global error pages that are shown to your user <strong>in</strong> case of an<br />

error.<br />

1. If you didn’t add the Global.asax file to your site <strong>in</strong> Chapter 11, you need to add it first. To do<br />

so, right-click the website <strong>in</strong> the Solution Explorer <strong>and</strong> choose Add Í Add New Item. In the list<br />

with templates, select Global Application Class. Leave its name set to Global.asax <strong>and</strong> click Add.<br />

2. At the top of the Global.asax file, right after the Application directive, add the follow<strong>in</strong>g<br />

Import statement. Note that when add<strong>in</strong>g an Import statement <strong>in</strong> Markup View, both <strong>VB</strong>.<strong>NET</strong><br />

<strong>and</strong> C# use the keyword Import, rather than Imports <strong>and</strong> us<strong>in</strong>g that you normally use <strong>in</strong> Code<br />

Beh<strong>in</strong>d:<br />

<strong>VB</strong>.<strong>NET</strong><br />

<br />

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!