MVC ActionLink将路由作为查询字符串而不是路由值附加

时间:2015-07-28 06:28:13

标签: c# asp.net asp.net-mvc asp.net-mvc-routing actionlink

我有一个MVC行动链接:

@Html.ActionLink("Update Information", "Index", "Performance", 
new { performanceid = item.PerformanceId }, null)

此操作链接的href如下所示:/ Performance / Index?performanceid = 100

在我的RouteConfig.cs中,我按以下顺序排列以下路径:

routes.MapRoute(
      "ShowPerformanceOptions",
      "Performance/{performanceid}/Index",
      new { controller = "Peformance", action = "Index" }
);

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

我不希望在URL的末尾添加查询字符串,我希望URL看起来像这样:/ Performance / 360 / Index

我经历过各种不同的选择,包括添加路由参数和可选的url参数,以及改变我编写ActionLink的方式。似乎没什么用。

任何想法?

2 个答案:

答案 0 :(得分:2)

要根据路由名称生成网址,请使用Html.RouteLink()方法

@Html.RouteLink("Update Information", "ShowPerformanceOptions", new { performanceid = item.PerformanceId })

好读What's the difference between RouteLink and ActionLink in ASP.NET MVC?

答案 1 :(得分:2)

正如@Satpal指出的那样,由于路线中的拼写错误,ActionLink无法正常工作:

routes.MapRoute(
      "ShowPerformanceOptions",
      "Performance/{performanceid}/Index",
      new { controller = "**Peformance**", action = "Index" }
);

routes.MapRoute(
      "ShowPerformanceOptions",
      "Performance/{performanceid}/Index",
      new { controller = "**Performance**", action = "Index" }
);