2011-02-07 12 views
0

我創建一個重定向到像這樣的行動......使用數據上QuerySting MVC

Response.Redirect(String.Format("~/Error/{0}/?message={1}", action, exception.Message)); 

如何我有exception.Message可供操作方法,當我重定向?

public ActionResult MyAction() 

回答

3

你需要在你的行動收到的查詢字符串PARAM參數。

return RedirectToAction("Your_Action_Name", new { msg = exception.Message}); 

你的行動:

public ActionResult Your_Action_Name(string msg) 
+0

謝謝,進出口新的MVC我沒有意識到的ActionResult參數必須是相同的變量名作爲查詢字符串參數。 – 2011-02-07 21:29:36

0

當您重定向時,您只能擁有查詢字符串參數。所以:

Response.Redirect(String.Format("~/Error/{0}/?message={1}", action, exception.Message)); 

和:

public ActionResult MyAction(string message) 
{ 
    ... 
} 

這就是說,在一個ASP.NET MVC應用程序中使用Response.Redirect和硬編碼的網址似乎只是非常錯誤。不幸的是,你沒有提供任何關於你想要做什麼的背景,所以我不確定我能否給你提供更好的建議,不要在ASP.NET MVC應用程序中使用Response.Redirect。使用URL傭工和類似行動的結果:

public ActionResult Foo() 
{ 
    ... 
    return RedirectToAction(action, "error", new { message = ex.Message }); 
} 

如果你想實現比你可能使用Application_Error事件的一些全局錯誤處理程序(你應該在的方式你的問題中提到這一點),那麼你可以有在電線之間的東西:

var routeData = new RouteData(); 
routeData.Values["controller"] = "error"; 
routeData.Values["action"] = action; 
routeData.Values["exception"] = exception; 
IController errorController = new ErrorController(); 
errorController.Execute(new RequestContext(new HttpContextWrapper(Context), routeData)); 

等,等,等...

相關問題