使用参数和模型绑定器重定向到操作不起作用

时间:2013-07-17 21:33:15

标签: asp.net-mvc asp.net-mvc-3 model-binding

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

有人能帮助我吗?

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);
}

2 个答案:

答案 0 :(得分:0)

我建议改用Controller.RedirectToAction。它有很多重载,使您可以使用任何相关的参数重新路由到您需要的Controller / Action。

答案 1 :(得分: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.Result = new RedirectToRouteResult(new RouteValueDictionary(new { 
    action = "Error", 
    controller = "Home", 
    exceptionInfo = ExceptionInfo
  }));
}

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