Click here to Skip to main content
13,597,272 members
Click here to Skip to main content
Add your own
alternative version


10 bookmarked
Posted 22 Feb 2012
Licenced CPOL

ASP.NET Health Monitor and Custom Errors

, 22 Feb 2012
Rate this:
Please Sign up or sign in to vote.
Enable email notifications on errors, and create custom pages for handling errors in MVC

Monitoring is an important part of the project life, ASP.NET provides features that make it easy to monitor your web app while it is in production, it is called ASP.NET Health Monitoring.

Enabling Health Monitor

Create a new ASP.NET MVC 3 Web Application and edit the Web.Config file, add the following between the <system.web> tags:

<!--<span class="code-comment"> Health Monitoring --></span>
  <healthMonitoring enabled="true">
      <clear />
      <!--<span class="code-comment"> Email WebEvents Provider --></span>
      <add name="EmailWebEventProvider"
  bodyHeader="My Monitor" buffer="false" />
      <clear />
      <add name="All Errors" type="System.Web.Management.WebBaseErrorEvent"
      startEventCode="0" endEventCode="2147483647" />
      <clear />
      <add name="All Errors Default" eventName="All Errors"
  provider="EmailWebEventProvider" profile="Default" minInstances="1"
  maxLimit="1000" minInterval="23:59:00" />

Here we have enabled health monitoring with an Email Alert for any error that occurs but we will only be notified once a day for each specific error, as we have set minInterval=”23:59:00?.

To complete the configuration, go ahead and set the “from”, “to”, and “header” properties in the above code, for more details on the health monitor properties, go here.


At this point, it will use the default local host smtp server, if you don't have an SMTP server running, you can set it up to connect to any available server, here we are using gmail SMTP server as I have a gmail account (500 email/day limit).

Add the following after the <system.web> tags:

<!--<span class="code-comment">SMTP --></span>
      <smtp from="" deliveryMethod="Network">
        <network defaultCredentials="false" enableSsl="true" host="" 

    password="MY_PASSWORD" port="587" userName="" />

Enter your email and password in the “password” and “userName” fields.


To trigger an error, we will add the following code to the “HomeController”:

Function Index() As ActionResult
ViewData("Message") = "Welcome to ASP.NET MVC!"

Throw New ArgumentException("ERROR FROM HomeController")

Return View()
End Function

Open the site and you will get an error, and an email notification.

Handling Errors

To suppress these errors and show our own custom page, we have to do some extra work, in our global.asax file, we add a global error handler, then we create views for each of the different types of errors you want to have a custom page for.

Open Global.asax and add the following code:

Private Sub MvcApplication_Error(sender As Object, e As System.EventArgs) _
    Handles Me.Error
Dim routeData = New RouteData
routeData.Values.Add("controller", "Home")
Dim lastEx = Server.GetLastError
If TypeOf (lastEx) Is HttpException Then
Dim ex As HttpException = lastEx
Select Case ex.GetHttpCode
Case 404
routeData.Values.Add("action", "Error404")
Case 500
routeData.Values.Add("action", "Error500")
Case Else
routeData.Values.Add("action", "ErrorGeneric")
End Select
Else 'not an Http Ex.
routeData.Values.Add("action", "ErrorGeneric")
End If
routeData.Values.Add("exception", lastEx)
Dim errController As IController = New ErrorController
errController.Execute(New RequestContext(New HttpContextWrapper(Context), routeData))
End Sub

In the above code, we handle the Me.Error global event, then we choose which view we want to show based on the HttpCode, and Exception type, we also put our Exception in routeData (optional) so it will be available to us in our View, clear the error that tells ASP.NET that we handled it, and then return one of the Views from the ErrorController. Let’s create the ErrorController.


Create a new controller, name it “ErrorController”, and add the following code:

Public Function Error404() As ActionResult
Response.StatusCode = 404
Dim ex As Exception = Nothing
If RouteData.Values.ContainsKey("exception") Then
ex = RouteData.Values("exception")
End If
Return View(ex)
End Function
Public Function Error500() As ActionResult
Response.StatusCode = 500
Dim ex As Exception = Nothing
If RouteData.Values.ContainsKey("exception") Then
ex = RouteData.Values("exception")
End If
Return View(ex)
End Function
Public Function ErrorGeneric() As ActionResult
Dim ex As Exception = Nothing
If RouteData.Values.ContainsKey("exception") Then
ex = RouteData.Values("exception")
End If
Return View(ex)
End Function

Here we have 3 views, one for 404 (Not Found), another for 500 server error, and a default generic error handler. We inspect if we passed in the exception in routeData and pass it to our view as the view model type.

Right click on the “Shared” folder and create the following 3 views.


@ModelType Exception

ViewData("Title") = "Error 404 : Page not found"

End Code
My Custom Error Page




Save as above.


Save as above.

Now if we rebuild and open the site again, we will see the following screen:


At this point, your site has been setup to email you when errors occur in your app, and if they do, the visitor will see a nice custom error page. With this in your app, you can handle more complex scenarios like having a 404 page that inspects the Exception passed in and tries to figure out what the visitor was trying to find in our site and give them some suggestions.

You can download the full sample project here.

I hope you like this post and for my next post, I will be looking at creating a whole site using ASP.NET localization features.

Filed under: .NET, ASP.NET, MVC, VB.NET


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Software Developer (Senior) KUFPEC
Kuwait Kuwait
No Biography provided

You may also be interested in...


Comments and Discussions

GeneralMy vote of 5 Pin
manoj kumar choubey19-Mar-12 19:34
membermanoj kumar choubey19-Mar-12 19:34 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web01-2016 | 2.8.180621.3 | Last Updated 22 Feb 2012
Article Copyright 2012 by Buaziz
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid