Global.asax
Code Snippet
- /// <summary>
- /// Handle application error on a global level.
- /// Passes handling off to the ErrorController
- /// </summary>
- protected void Application_Error()
- {
- var exception = Server.GetLastError();
- var httpException = exception as HttpException;
- Response.Clear();
- Server.ClearError();
- routeData.Values["controller"] = "Errors";
- 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;
- }
- }
- errorsController.Execute(rc);
- }
End of Code Snippet
Note: This will route our errors to the ErrorController. Lets take a look at the error controller...
ErrorController.cs
Code Snippet
- // <copyright file="ErrorController.cs" company="GinkoSolutions.com">
- // Copyright (c) 2011 All Right Reserved
- // </copyright>
- // <author>Sean Greasley</author>
- // <email>sean@ginkosolutions.com/sean@tutorialgenius.com</email>
- // <summary>Controller for handling errors within the application.</summary>
- namespace MVCEmailExample.Controllers
- {
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Web.Mvc;
- using MVCEmailExample.Models;
- /// <summary>
- /// Controller for handling errors within the application.
- /// </summary>
- public class ErrorController : Controller
- {
- public ActionResult General(Exception exception)
- {
- return View("Error", new ErrorModel() { ErrorTitle = "General Error", ExceptionDetail = exception });
- }
- public ActionResult Http404()
- {
- }
- public ActionResult Http403()
- {
- }
- }
- }
End of Code Snippet
So now were handling our errors and throwing them out to the Error View (in Views/Shared!). However, were now using a strongly typed view and passing a custom class into it. This custom class contains details of our error!
ErrorModel.cs (Our custom error class!)
Code Snippet
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- namespace MVCEmailExample.Models
- {
- public class ErrorModel
- {
- public string ErrorTitle { get; set; }
- public Exception ExceptionDetail { get; set; }
- }
- }
End of Code Snippet
Very simple! All it does it holds information really.
Now lets take a look at our error view...
Error View (This will already exist with a new MVC3 application) P.s. I'm using Razor syntax!
Code Snippet
- @using MVCEmailExample.Models
- @model ErrorModel
- @{
- Layout = "~/Views/Shared/_Layout.cshtml";
- }
- Sorry, an error occurred while processing your request.
- <br />
- @if (Model.ExceptionDetail != null)
- {
- @Model.ExceptionDetail.Message
- }
End of Code Snippet
This is our strongly typed view (Hint: @model ErrorModel). We simply extract the error info here and display it in a very (unstylish) form!
This sample below is for a sample email application. This uses the code described above. Just hit the button without entering any information and (assuming you don't have local mail server) u'll see some errors appearing!
MVC3 Razor - Global Error Handling
6 comments:
Works like a charm! Nice going, decent piece of work!
Works perfectly.
thanks for the tutorial.
i have one problem with this code.
i'm trying to provide a link in the error view, which will take the user to the home page.
but every time i click on the link it takes me to the same error module again and i end up back in the error view.
i cant seem to navigate away from this error page with the help of a link.i tried different ways of doing this. but so far no luck.
any comments will be appreciated.
Should probably use the System.Web.Mvc.HandleErrorInfo rather than creating a new custom view model (ErrorModel).
Not working. When you put http://domain.com/http://domain.com then all path are illegal and routes are invalid.
Nice! Works great. Didn't know it was this easy!
Appreciated.
Very well done!
Post a Comment