Skip to content

Instantly share code, notes, and snippets.

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.
public void Application_Error(object sender, EventArgs e)
filterContext.HttpContext.Response.ClearHeaders();
filterContext.HttpContext.Response.ClearContent();
filterContext.HttpContext.Response.Clear();
public class DerivedHandleErrorAttribute : HandleErrorAttribute
{
public override void OnException(ExceptionContext filterContext)
{
base.OnException(filterContext);
if (context.ExceptionHandled)
{
// Log filterContext.Exception in some way.
}
new HandleErrorAttribute
{
View = "SomeView",
ExceptionType = typeof (SomeException)
}
public static void RegisterGlobalFilters(GlobalFilterCollection filters)
{
filters.Add(new HandleErrorAttribute());
}
Response.TrySkipIisCustomErrors = true;
<httpErrors errorMode="Custom" existingResponse="Replace">
<remove statusCode="500" />
<error
statusCode="500"
path="/MvcErrorHandling/Error/HttpErrors500"
responseMode="ExecuteURL" />
</httpErrors>
<system.webServer>
<httpErrors errorMode="DetailedLocalOnly" existingResponse="Auto" />
<!-- any other elements -->
</system.webServer>