2013-03-15 73 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的您设置的默认操作为“组件”,我认为,链接生成是足够聪明,把它关闭。