Asp.net mvc 2 如何防止“Index”操作出现在我的ASP.NET MVC2 URL中?

Asp.net mvc 2 如何防止“Index”操作出现在我的ASP.NET MVC2 URL中?,asp.net-mvc-2,url-routing,asp.net-mvc-routing,Asp.net Mvc 2,Url Routing,Asp.net Mvc Routing,我有一条看起来像这样的路线(但我试过几种不同的): 在MetricsController上,我有以下操作: // Index sets up our UI public ActionResult Index(int id, string resource, string period) // GetMetrics returns json consumed by ajax graphing tool public JsonResult GetMetrics(int id, string res

我有一条看起来像这样的路线(但我试过几种不同的):

MetricsController
上,我有以下操作:

// Index sets up our UI
public ActionResult Index(int id, string resource, string period)

// GetMetrics returns json consumed by ajax graphing tool
public JsonResult GetMetrics(int id, string resource, string period)
我如何生成链接并且在url中没有
索引
,以便我可以这样做

/Metrics/1234/cpu-0/10m -> MetricsController.Index(id, string resource, string period) /Metrics/GetMetrics/1234/cpu-0/10m -> MetricsController.GetMetrics(int id, string resource, string period) /指标/1234/cpu-0/10m-> 索引(id、字符串资源、字符串周期) /Metrics/GetMetrics/1234/cpu-0/10m-> GetMetrics(整数id、字符串资源、字符串周期)
我尝试了各种各样的
Html.ActionLink
Html.RouteLink
咒语,但毫无乐趣。

您可能需要两条路由,因为可选参数只能位于路由定义的末尾:

routes.MapRoute(
    "ActionlessMetrics",
    "Metrics/{id}/{resource}/{period}",
    new { controller = "Metrics", action = "Index" }
);

routes.MapRoute(
    "Metrics",
    "Metrics/{action}/{id}/{resource}/{period}",
    new { controller = "Metrics" }
);
并生成这些链接:

<%= Html.ActionLink("Link1", "Index", "Metrics", 
    new { id = 123, resource = "cpu-0", period = "10" }, null) %>
<%= Html.ActionLink("Link2", "GetMetrics", "Metrics", 
    new { id = 123, resource = "cpu-0", period = "10" }, null) %>

这将导致:

<a href="/Metrics/123/cpu-0/10">Link1</a>
<a href="/Metrics/GetMetrics/123/cpu-0/10">Link2</a>


并且选择了正确的操作。

hmmm在mvc2中,这种方法实际上似乎并不适合我。不管url中仍然显示什么操作方法名称,不管是否有如上所述的路由。Html.ActionLink仍会放入索引,例如:


仍然会在链接中生成索引。

谢谢Darin,我认为这可能是一种方法,但不知道是否可以只使用一条路线。这意味着,可以想象,在一个相当大的应用程序上,你最终可能会遇到路由爆炸?@Kev,是的,这是可能的,特别是如果你是某个SEO狂热者的话。就我个人而言,我不介意在url中添加
索引
,以避免重复路由,但这是主观的。在大型应用程序中,可能会有一些区域允许您按功能对URL进行分组。
<a href="/Metrics/123/cpu-0/10">Link1</a>
<a href="/Metrics/GetMetrics/123/cpu-0/10">Link2</a>