in

ASPXWizard.net

.net and Ajax Community

Mina Labib

ASP.NET MVC handle exceptions

/* You can also find more posts on my technical blog, and follow me at twitter @minalabib */

ASP.NET MVC has a cool and neat way to catch and handle exceptions, I will show here a quick review for handling exceptions in MVC framework and I will present small addition I added too.

The main idea here is all processes and work flow should go through a Controller and an Action method inside that controller, so if any exception or error happened in any step of the work flow (DAl, BL, action, model, server side validation, etc … ) it should end up at the Controller/Action level, ASP.NET MVC provide a nice technique with OnException even handler which can be overridden in your controller, and thus all exception that is bubbled up to the action will be caught and handled in this method.

Ok, enough talk let’s code, I have a base controller that all my controllers inherit from and I believe it good practice:

public class ApplicationControllerBase : Controller

 

and inside that base controller I override the OnException event handler

protected override void OnException(ExceptionContext filterContext)
{
    base.OnException(filterContext);
    // TODO: Handle exception
    // Log in file or database for example
   
ArrayList TempArrayList = new ArrayList();
    TempArrayList.AddRange(ConfigurationSettings.AppSettings["ToBeViewed"].Split(','));
    // to pass a toggle that tells
    // the view to show the exception message or not.
   
if (TempArrayList.Contains(filterContext.Exception.GetType().Name))
        filterContext.Exception.Data.Add("IsViewable", true);
    else
       
filterContext.Exception.Data.Add("IsViewable", false);
     // to tell .NET framework the exception is
   
// handled and does not show yellow exception page.
   filterContext.ExceptionHandled = true;
    // to direct to the error view,
    // passing the exception as a model.
   
this.View("Error", filterContext.Exception).ExecuteResult(this.ControllerContext);
}

In the method we should handle exception according to our needs (log the exception for example), and then tells the framework that the exception is handled and not to show the ugly yellow exception page of .NET by using (filterContext.ExceptionHandled = true; ).

Also I built a comma separated list of exception names in the web.config file, and if the caught exception is one of these specified exceptions, system will send a flag to the error page to show the exception message or not.

And then the context will redirect to the error view (by default in MVC this page is generated Views/Shared/Error.aspx), I edited this view to accept System.Exception as model and view the error message according to toggle preset in the exception object:

<%@PageLanguage="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<System.Exception>"%>
<asp:ContentID="errorTitle"ContentPlaceHolderID="TitleContent"runat="server">
  
Error Page      
</asp:Content>
<
asp:ContentID="errorContent"ContentPlaceHolderID="MainContent"runat="server">
    <
h2>
   Sorry, an error occurred while processing your request.
 
    </h2>
  
<% if ((bool)Model.Data["IsViewable"]){ %>
    <div>
      
<%=Model.Message %>
    </div>
  
<% } %>
</asp:Content>

Thanks for this post for help in that.

Comments

No Comments

Leave a Comment

(required)  
(optional)
(required)  
Add
ASPXWizard.net some rights reserved 2005-2007
Powered by Community Server (Non-Commercial Edition), by Telligent Systems