Skip to content

Instantly share code, notes, and snippets.

new HandleErrorAttribute
{
View = "SomeView",
ExceptionType = typeof (SomeException)
}
public class DerivedHandleErrorAttribute : HandleErrorAttribute
{
public override void OnException(ExceptionContext filterContext)
{
base.OnException(filterContext);
if (context.ExceptionHandled)
{
// Log filterContext.Exception in some way.
}
filterContext.HttpContext.Response.Clear();
filterContext.HttpContext.Response.ClearHeaders();
filterContext.HttpContext.Response.ClearContent();
public void Application_Error(object sender, EventArgs e)
public class ErrorHandlerHttpModule : IHttpModule
{
public void Init(HttpApplication context)
{
context.Error += Application_Error;
}
private static void Application_Error(object sender, EventArgs e)
{
// Error handling code goes here.
<system.web>
<!-- Additional configuration elided. -->
<httpModules>
<add
name="ErrorHandlerHttpModule"
type="MvcErrorHandling.MvcHelpers.ErrorHandlerHttpModule, MvcErrorHandling" />
</httpModules>
</system.web>
<system.webServer>
var exception = HttpContext.Current.Server.GetLastError();
var httpException = exception as HttpException;
...
var statusCode = httpException.GetHttpCode();
HttpContext.Current.Server.ClearError();