0

这是我的代码。当我将响应重定向到某个操作时,模型绑定器不起作用。

有谁能够帮我 ?

public void OnException(ExceptionContext filterContext)
{
  var action = filterContext.RouteData.Values["action"];
  var controller = filterContext.RouteData.Values["controller"];
  var values = filterContext.RouteData.Values;
  var exceptionMessage = filterContext.Exception.Message;
  var exception = filterContext.Exception;

  filterContext.ExceptionHandled = true;
  ExceptionInfo ExceptionInfo = new ExceptionInfo();
  ExceptionInfo.ActionName = action.ToString();
  ExceptionInfo.ControllerName = controller.ToString();
  ExceptionInfo.ExceptionMessage = exceptionMessage;

  filterContext.HttpContext.Response.RedirectToRoute(new
  {
    action = "Error",
    controller = "Home",
    ExceptionInfo = ExceptionInfo
  });
}

public ActionResult Error(ExceptionInfo ExceptionInfo)
{
  return View(ExceptionInfo);
}
4

2 回答 2

1

将您的代码替换为以下内容:

public void OnException(ExceptionContext filterContext)
{
  var action = filterContext.RouteData.Values["action"];
  var controller = filterContext.RouteData.Values["controller"];
  var values = filterContext.RouteData.Values;
  var exceptionMessage = filterContext.Exception.Message;
  var exception = filterContext.Exception;

  filterContext.ExceptionHandled = true;
  ExceptionInfo ExceptionInfo = new ExceptionInfo();
  ExceptionInfo.ActionName = action.ToString();
  ExceptionInfo.ControllerName = controller.ToString();
  ExceptionInfo.ExceptionMessage = exceptionMessage;

  filterContext.Result = new RedirectToRouteResult(new RouteValueDictionary(new { 
    action = "Error", 
    controller = "Home", 
    exceptionInfo = ExceptionInfo
  }));
}

public ActionResult Error(ExceptionInfo exceptionInfo)
{
  return View(exceptionInfo);
}
于 2013-07-17T23:27:02.307 回答
0

我建议改用Controller.RedirectToAction。它有很多重载,应该使您能够使用任何相关的参数重新路由到所需的控制器/操作。

于 2013-07-17T21:55:06.690 回答