2012-05-28 56 views
86

我有兩個控制器,這兩個叫AccountController。其中之一,讓我們叫它Controller A,在AreaAdmin和其他,讓我們叫它Controller B,是不是在任何Area(我想這意味着它是在默認Area?)。 Controller B有一個action method調用Login。我有一個Controller Aaction method,其此行重定向到行動中另一個控制器

return RedirectToAction("LogIn", "Account"); 

的問題是,我得到一個404時因爲試圖重定向到一個不存在的actionController A此行得到執行。我想致電Controller B中的action method。這可能嗎?

+1

可能重複http://stackoverflow.com/questions/7892094/mvc-redirect- to-index-from-another-controller) – musefan

回答

173

您可以在routeValues參數提供area。試試這個:這取決於你的目標的哪個區域

return RedirectToAction("LogIn", "Account", new { area = "Admin" }); 

或者

return RedirectToAction("LogIn", "Account", new { area = "" }); 

+0

如果我想從某個區域的視圖轉到不在任何區域的控制器的操作,該怎麼辦。與MVC5中一樣,右上角的LogOff按鈕位於AccountController中,該按鈕位於任何區域。我想從某個區域的視圖中退出? –

+1

我的第二個例子,'area =「」',會爲你做。 –

+0

三江源非常 –

17

使用此:

return RedirectToAction("LogIn", "Account", new { area = "" }); 

這將重定向到Account控制器LogIn行動在「全局」區域。

它使用此RedirectToAction過載:

protected internal RedirectToRouteResult RedirectToAction(
    string actionName, 
    string controllerName, 
    Object routeValues 
) 

MSDN

[MVC重定向到索引從另一個控制器](的
相關問題