Handling errors in ASP.NET MVC


The HandleErrorAttribute let us specify how to handle an exception that is thrown by an action method. By default, when an action method with the HandleErrorAttribute throws an exception, ASP.NET MVC displays the “Error.cshtml” view that is located in the “/Views/Shared” folder.

public static void RegisterGlobalFilters(GlobalFilterCollection filters) {
    filters.Add(new HandleErrorAttribute());
}

This filter automatically applies to any action method in any controller so that you don’t have to apply HandleErrorAttribute action or controller level.

If user makes a request for “web.config” file and we know that ASP.NET MVC restricts this request and throws an exception that is uncatchable by HandleErrorAttribute because HandleErrorAttribute just handles errors that are thrown by action methods. So for above case, traditional ASP.NET error window will be displayed. Here is a diagram that shows how HandleErrorAttribute works:

So how to handle errors that are not thrown by any action method means that errors those are unhandled or uncaught-able by HandleErrorAttribute.

It is simple, just create a new file “Error.aspx” in the root of the project and update “web.config” file with this code snippet:

<!-- Unhandled Error Section -->
<customerrors mode="On" redirectmode="ResponseRewrite" defaultredirect="Error.aspx" />

Now, whenever an error thrown by any action method, HandleErrorAttribute are asked to handle it and “Error.cshtml” view will be displayed. In contrast, when an unhandled error will occur that is not thrown by any action method, “Error.aspx” page will be displayed.

If you want to know what unhandled error was and you want to be informed through email, it is simple, just do it:

/* for Sending Error through Email */
protected void Page_Load(object sender, EventArgs e) {
    string subject = "An unhandled Error Message";
    string body = HttpContext.Current.Server.GetLastError().ToString(); 
    subject.SendErrorMessage(body);
}
public static void SendErrorLive(this string subject, string body) {
    WebMail.From = "UserName@live.com";
    WebMail.Password = "password";
    WebMail.SmtpPort = 25;
    WebMail.SmtpServer = "smtp.live.com";
    WebMail.UserName = "UserName@live.com";
    WebMail.EnableSsl = true;
    WebMail.SmtpUseDefaultCredentials = false;
    WebMail.Send("UserName@live.com", subject, body);
}

This technique is for “Error.aspx” page but what for “Error.cshtml” view?

By default, asp.net mvc passes HandleErrorInfo type to the “Error.cshtml” view. This type let us directly get error info as shown here:

@Model.Exception.Message

Or directly send email within the view:

@{
    Model.Exception.Message.SendErrorMessage(Model.Exception.ToString());
}

Now following errors are under your control:

  • Error.cshtml: Errors that are thrown by action methods
  • Error.aspx: Any other kind of errors
Advertisements

One thought on “Handling errors in ASP.NET MVC

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s