2013-03-15 79 views
0

我正在使用ASP.NET MVC 4根據路由配置沒有給出想要的結果的url生成

我有一個名爲Server的控制器,以及2個動作方法,分別叫做SearchComponent。我有以下的路由配置:

routes.MapRoute("Component", 
    "{controller}/{serverId}/{action}", 
    new { controller = "Server", action = "Component" }, 
    new { serverId = @"\d+" }); 

我找類似網址:

/Server/12345/Component 

我的搜索操作方法:

return RedirectToAction("Component", new { serverId = 12345 }); 

我的組件的操作方法:

public ActionResult Component(int serverId) 
{ 
    return View(); 
} 

The生成的網址是:

/Server/12345/ 

這是錯誤的,它是遺漏了「組件」。爲什麼是這樣?

+1

在一個側面說明,這是一個偉大的事情來測試你的路線:http://haacked.com/archive/2008/03/13/url-routing-debugger.aspx – Yahya 2013-03-15 10:37:07

回答

1

您將組件定義爲Default-Action,爲什麼要添加它? 如果您需要它,請將其從默認位置移除並將其添加到您的RedirectToAction調用中。

3
 new { controller = "Server", action = "Component" }, 

becase的您設置的默認操作爲「組件」,我認爲,鏈接生成是足夠聰明,把它關閉。