Skip to content

Instantly share code, notes, and snippets.

@johnkchow
Created February 10, 2011 22:35
Show Gist options
  • Save johnkchow/821508 to your computer and use it in GitHub Desktop.
Save johnkchow/821508 to your computer and use it in GitHub Desktop.
void Application_Error(object sender, EventArgs e)
{
Exception ex = Server.GetLastError();
if (ex.GetType() != typeof(HttpException))
{
if (ex.GetType() == typeof(HttpUnhandledException) && ex.InnerException != null)
ex = ex.InnerException;
CoreManager.ExceptionService.HandleException(ex);
}
else if (ex.GetType() == typeof(HttpException))
{
HttpException httpEx = ex as HttpException;
if (httpEx != null && httpEx.GetHttpCode() == 404)
{
RouteData routeData = new RouteData();
routeData.Values.Add("controller", "Error");
routeData.Values.Add("action", "HttpError404");
routeData.Values.Add("exception", ex);
Server.ClearError();
Response.TrySkipIisCustomErrors = true;
IController errorController = new ErrorController();
errorController.Execute(new RequestContext(new HttpContextWrapper(Context), routeData));
}
}
}
//... In the ErrorController file...
public class ErrorController : Controller
{
public ActionResult HttpError404()
{
Response.StatusCode = 404;
return View();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment