2011-04-15 57 views
0

我有一個控制器,它有一個名爲RedirectLogin的操作,它基本上將TempData var設置爲重定向到登錄完成時。asp mvc RedirectToRoute將當前操作添加到重定向

在此之後,它只是簡單地調用:

return RedirectToRoute("Login"); 

然而,當它重定向,它進入/登錄/ RedirectLogin - 它是在調用動作的名稱,套結出於某種原因。我認爲它應該只是返回默認值,而不是添加動作,特別是不屬於目標控制器的動作。

如果我稱之爲:

return RedirectToRoute("Login", new { action = "Index" }); 

然後正確地使路徑/登錄,但同樣,不應該是做在默認情況下,沒有額外的位?

我有一個稍微不標準的路由設置。基本上我爲我的客戶創建虛擬子文件夾,所以www.domain.com/clienturl,但我仍然希望www.domain.com/join或www.domain.com/about重定向到非客戶端數據。我通過直接在路徑中添加控制器的名稱來完成此操作,並在名爲Landing的更通用的控制器之前添加它。難道這是問題,或者是有沒有更好的辦法做到這一點:)

 // Root paths 
     routes.MapRoute("About", "about", new { controller = "Home", action = "About" }); 
     routes.MapRoute("Info", "info", new { controller = "Home", action = "Info" }); 
     routes.MapRoute("Privacy", "privacy", new { controller = "Home", action = "Privacy" }); 
     routes.MapRoute("Terms", "terms", new { controller = "Home", action = "Terms" }); 

     // Root controllers 
     routes.MapRoute("Join", "join/{action}/{id}", new { controller = "join", action = "Index", id = UrlParameter.Optional }); 
     routes.MapRoute("Login", "login/{action}/{id}", new { controller = "login", action = "Index", id = UrlParameter.Optional }); 
     routes.MapRoute("Account", "account/{action}/{id}", new { controller = "account", action = "Index", id = UrlParameter.Optional }); 
     routes.MapRoute("Goodbye", "goodbye/{action}/{id}", new { controller = "goodbye", action = "Index", id = UrlParameter.Optional }); 

     // Organization 
     routes.MapRoute("Landing", "{organization}/{controller}/{action}/{id}", new { controller = "Landing", action = "Index", id = UrlParameter.Optional }); 

     // Default 
     routes.MapRoute("Default", "{controller}/{action}/{id}", new { controller = "Home", action = "Index", id = UrlParameter.Optional }); 

回答

2

它重用數據從當前請求。因爲你有可選參數'action',它會從當前請求中獲取操作參數,操作是RedirectLogin,並將其附加到url。我會在沒有可選參數的情況下爲您的登錄頁面創建特定的路由條目,或者重定向到操作而不使用命名的路由。 Pro ASP.NET MVC 2中有一個頁面,更詳細地解釋了發生了什麼。

1

替代你不妨試試如下:

RedirectToAction(String, String, RouteValueDictionary); 
RedirectToAction(String, RouteValueDictionary); 
Redirect("~/login"); // Note, this works but if you move around the urls for your routes you'll need to fix it everytime... not recommended 

MSDN文章在這裏顯示了所有可用的在一個控制器方法:http://msdn.microsoft.com/en-us/library/system.web.mvc.controller_methods%28v=VS.98%29.aspx

不要使用RedirectToActionPermanent或RedirectToRoutePermanent爲此,雖然...