public class UserFriendlyExceptionHandler : HandleErrorAttribute
{
public override void OnException(ExceptionContext filterContext)
{
...
var area = filterContext.Controller.ControllerContext.RouteData.DataTokens["area"];
var controller = filterContext.Controller.ControllerContext.RouteData.Values["controller"];
var action = filterContext.Controller.ControllerContext.RouteData.Values["action"];
NameValueCollection queryString = filterContext.HttpContext.Request.QueryString;
RouteValueDictionary redirectTargetDictionary = queryString.ToRouteValueDictionary();
redirectTargetDictionary.Add("area", area);
redirectTargetDictionary.Add("controller", controller);
redirectTargetDictionary.Add("action", action);
filterContext.Result = new RedirectToRouteResult(redirectTargetDictionary);
...
}
当我在更新或插入中遇到异常时我想打印此异常并重定向回同一个控制器,但问题是filterContext.Result
将被重定向回具有查询参数的正确控制器但是我我得到无限循环。回到控制器和异常,回到控制器和异常,......
为什么呢?如果抛出错误,我怎么能回到控制器?