Last active
August 29, 2015 13:55
-
-
Save andrewbranch/8785460 to your computer and use it in GitHub Desktop.
Show a custom error page for a custom exception. For example, when trying to access a non-existent database record (app/people/details/9999999).
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// You could put more information and functionality here if you want. | |
// I just needed an exception distinguishable from other exceptions. | |
// I implemented a "FindOrDie" method in my repository code, from which | |
// I throw this exception whenever a query comes up empty. | |
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Web; | |
namespace Inspect.Models { | |
[Serializable] | |
public class EntityNotFoundException : Exception { | |
public EntityNotFoundException(string message) : base(message) { } | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Inspect.Models; | |
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Web; | |
using System.Web.Mvc; | |
namespace Inspect.Utilities { | |
public class EntityNotFoundHandleErrorAttribute : HandleErrorAttribute { | |
public override void OnException(ExceptionContext filterContext) { | |
if (!filterContext.IsChildAction && !filterContext.ExceptionHandled && filterContext.HttpContext.IsCustomErrorEnabled && filterContext.Exception is EntityNotFoundException) { | |
string controllerName = (string)filterContext.RouteData.Values["controller"]; | |
string actionName = (string)filterContext.RouteData.Values["action"]; | |
HandleErrorInfo model = new HandleErrorInfo(filterContext.Exception, controllerName, actionName); | |
ViewResult result = new ViewResult { | |
ViewName = this.View, | |
ViewData = new ViewDataDictionary<HandleErrorInfo>(model), | |
TempData = filterContext.Controller.TempData | |
}; | |
filterContext.Result = result; | |
filterContext.ExceptionHandled = true; | |
filterContext.HttpContext.Response.Clear(); | |
filterContext.HttpContext.Response.StatusCode = 500; | |
filterContext.HttpContext.Response.TrySkipIisCustomErrors = true; | |
} | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// This registers the error handler application-wide. | |
// If you only want it to take effect on certain controllers or actions, | |
// you can decorate the class definition with [EntityNotFoundHandleError(View = "~/Views/Errors/EntityNotFound")] | |
// In that case it would probably make more sense to move that string inside the HandleErrorAttribute definition though. | |
using System.Web; | |
using System.Web.Mvc; | |
using Inspect.Utilities; | |
namespace Inspect { | |
public class FilterConfig { | |
public static void RegisterGlobalFilters(GlobalFilterCollection filters) { | |
filters.Add(new EntityNotFoundHandleErrorAttribute() { | |
ExceptionType = typeof(Inspect.Models.EntityNotFoundException), | |
View = "~/Views/Errors/EntityNotFound.aspx", | |
Order = 2 | |
}); | |
// Or the default error handler. Your custom handler should not replace | |
// the default handler unless because it's only written to intercept a specific exception type | |
filters.Add(new Utilities.ElmahHandleErrorAttribute(), 1); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment