根据路由配置,Url生成没有给出期望的结果

时间:2013-03-15 10:33:55

标签: c# asp.net asp.net-mvc asp.net-mvc-4

我正在使用ASP.NET MVC 4

我有一个名为Server的控制器,以及两个名为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();
}

生成的网址是:

/Server/12345/

这是错误的,它遗漏了“组件”。这是为什么?

2 个答案:

答案 0 :(得分:3)

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

因为您将默认操作设置为“Component”,所以我认为链接生成非常智能,可以将其关闭。

答案 1 :(得分:1)

您将Component定义为Default-Action,为什么要追加它? 如果您希望它在您的路线中,请将其从默认路线中删除并将其添加到您的RedirectToAction呼叫中。