2013-07-17 51 views
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); 
} 

回答

0

我建議使用Controller.RedirectToAction代替。有很多重載,它應該使您能夠重新路由到您需要的控制器/操作,並使用任何相關的參數。

+0

我試過,但相同的結果 –

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

它dosent作品 –

+0

究竟會發生什麼? – ataravati

+0

我得到了同樣的結果,null :(但是如果我用參數重定向它參數woks –