2012-01-26 83 views
2

我有一個MVC項目巫婆默認映射whcih我改成:MVC映射 - RedirectToAction不工作

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

以下控制器:

/Controllers/HomeController.cs:

public ActionResult Index(int? id) 
{   
    return RedirectToAction("Index", "EPOS"); 
} 

/Controllers/EPOSController.cs:

public ActionResult Index(int? id) 
{ 
    return View(); 
} 

斷點被擊中HomeController的,但在EPOSController不打,我得到這個錯誤:

No route in the route table matches the supplied values.

我在做什麼錯?

+0

用於產生什麼網址:返回RedirectToAction(「指數」, 「EPOS」); – epignosisx

+0

在瀏覽器「http:// localhost:1096 /' – gunwin

+0

中顯示錯誤時,請使用'RedirectToAction(」Index「,」EPOS「,null);'嘗試使用URL。讓我知道這是否會改變結果。 – 2012-01-26 20:46:05

回答

4

我相信這個問題是因爲你Default路線是不正確的:

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

你不能有一個可選的參數前述必需的參數。如果將上述路線更改爲此,會發生什麼情況?

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

你的建議也行不通,沒錯,這只是MVC 3中引入的一個bug。請看Phil Haack的這篇文章http://haacked.com/archive/2010/02/12/asp -net-mvc-2-optional-url-parameters.aspx – epignosisx

+0

@epignosisx其關於mvc2不是mvc3 –

+0

@Shark對不起,我給了你錯誤的鏈接,這是一個:http://haacked.com/archive/2011/ 2月20日/路由迴歸與 - 雙連續-可選-URL-parameters.aspx – epignosisx

0

我認爲鯊魚是正確的,試圖區分你的路線圖分爲兩個定義是這樣的(我沒有測試過這一點)

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