C#抛出异常并通过RedirectToRouteResult重定向回控制器给了我无限循环

时间:2014-10-29 10:07:26

标签: c# asp.net-mvc-4

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将被重定向回具有查询参数的正确控制器但是我我得到无限循环。回到控制器和异常,回到控制器和异常,......

为什么呢?如果抛出错误,我怎么能回到控制器?

0 个答案:

没有答案