2

I brand new to ASP.NET MVC3. How would I create a global custom error page for MVC3? The general idea is when an exception is thrown it would show a generic message to the user and it would log the exception to a database for developers to investigate later.

Thanks in advance for your help.

Here is what I ended up doing in global.asax.cs:

    protected void Application_Error()
    {
        var exception = Server.GetLastError();

        Log.Error("Exception", exception);

        var httpException = exception as HttpException;
        Response.Clear();
        Server.ClearError();
        var routeData = new RouteData();
        routeData.Values["controller"] = "Error";
        routeData.Values["action"] = "General";
        routeData.Values["exception"] = exception;
        Response.StatusCode = 500;
        if (httpException != null)
        {
            Response.StatusCode = httpException.GetHttpCode();
            switch (Response.StatusCode)
            {
                case 403:
                    routeData.Values["action"] = "Http403";
                    break;
                case 404:
                    routeData.Values["action"] = "Http404";
                    break;
            }
        }

        IController errorsController = new ErrorController();
        var rc = new RequestContext(new HttpContextWrapper(Context), routeData);
        errorsController.Execute(rc);
    }
Greg Finzer
  • 6,714
  • 21
  • 80
  • 125

3 Answers3

1

In your Global.asax file implement the Application_Error method:

    protected void Application_Error() { 
        HttpContext ctx = HttpContext.Current; 
        var error = ctx.Server.GetLastError();
        ctx.Response.Clear(); 
        ctx.Response.End(); 
    }

Following up on Maess' comment:

Read this: Error Handling in asp.net mvc 3

Community
  • 1
  • 1
Ropstah
  • 17,538
  • 24
  • 120
  • 194
1

What I've done in my project is I created a BaseController and overridden the OnException event as below,

protected override void OnException(ExceptionContext filterContext)
{
    // do some logging using log4net or signal to ELMAH
    filterContext.ExceptionHandled = true;
    var exModel = new HandleErrorInfo(filterContext.Exception,  
                  filterContext.RouteData.Values["controller"].ToString(), 
                  filterContext.RouteData.Values["action"].ToString());
    View("Error", exModel).ExecuteResult(ControllerContext);
}

Also I removed the HandleError action filter registered in the Global.asax.cs.

Note: You should have a view with name Error in shared folder.

Update: To extract the error information from the Error view you have to bind the Error view to the model HandleErrorInfo.

@model System.Web.Mvc.HandleErrorInfo

Then you can easily access the exception anywhere in the view as

@Model.Exception
VJAI
  • 32,167
  • 23
  • 102
  • 164
0

Create a view called Error and add it to your Views\Shared folder. Then in your catch blocks, after you have logged the error, redirect the action to the error view. If you want to display a sophisticated message create an Error model and pass it to the view, or put the information into the ViewBag.

For information on unhandled exceptions and HTTP errors see Ropstah's post.

Maess
  • 4,118
  • 20
  • 29