Skip to content

Instantly share code, notes, and snippets.

@imwower
Created July 30, 2014 03:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save imwower/b7c3a3b03d2f790e3495 to your computer and use it in GitHub Desktop.
Save imwower/b7c3a3b03d2f790e3495 to your computer and use it in GitHub Desktop.
public class MvcErrorAttribute : HandleErrorAttribute
{
public ILogger Logger { get; set; }
public ErrorController ErrorController { get; set; }
public MvcErrorAttribute()
{ }
public override void OnException(ExceptionContext filterContext)
{
if (filterContext.ExceptionHandled)
return;
filterContext.ExceptionHandled = true;
var exception = filterContext.Exception;
string controllername = filterContext.RouteData.Values["controller"] as string;
string actionname = filterContext.RouteData.Values["action"] as string;
Logger.Error(string.Format("error on filter context, controller : {0}, action : {1}", controllername, actionname), exception);
var routeData = new RouteData();
routeData.Values["controller"] = "Error";
routeData.Values["action"] = "Error";
var httpexception = exception as HttpException;
if (httpexception != null)
routeData.Values["code"] = httpexception.GetHttpCode();
var url = "http://localhot:8080/Error/Error"; //the urL of Error Controller
var str = "<script>location.href='" + url + "'</script>";
HttpContext.Current.ClearError();
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.Write(str);
HttpContext.Current.Response.End();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment