Skip to content

Instantly share code, notes, and snippets.

@andrewbranch
Last active August 29, 2015 13:55
Show Gist options
  • Save andrewbranch/8785460 to your computer and use it in GitHub Desktop.
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).
// 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) { }
}
}
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 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