c# - 为什么 RouteLink 会生成友好的 URL 而 ActionLink 不会?

标签 c# asp.net-mvc routes actionlink

我有一个关于 RouteLink 与 ActionLink 的问题。

考虑以下路线

routes.MapRoute("Routename1",
    "{someEnum}/SpecificAction/{id}/{stringId}",
    new { controller = "MyController", id = (int?)null, stringId= (string)null, action = "SpecificAction" },
    new { someEnum= "(EnumVal1|EnumVal2)" }
);

奇怪的 {someEnum} 部分是因为我对构成 url 的典型 Controller 部分的枚举的所有值使用通用 Controller 。例如,/EnumVal1/Action/和/EnumVal2/Action/使用相同的 Controller 。然而,这不是问题的一部分。

考虑以下两种链接方式:

<%=Html.RouteLink("Click me","Routename1", new { id = 32, stringId = "Yatzy" })%> 
<%=Html.ActionLink("Click me", "SpecificAction", "EnumVal1", new { id = 32, stringId = "Yatsy" }, null)%>

RouteLink 生成正确的 url,应该是/EnumVal1/SpecificAction/32/Yatzy

ActionLink 生成一个类似于/EnumVal1/SpecificAction/32?stringId=Yatzy 的 url

这是为什么?请有人向我解释一下。

最佳答案

RouteLink can only ever use the one route you specify . ActionLink 将使用第一个匹配的路由,无论它是否是您想要的。您的两个示例可能匹配不同的路线。

Phil Haack's routing debugger将有助于澄清这一点。

关于c# - 为什么 RouteLink 会生成友好的 URL 而 ActionLink 不会?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1920593/

相关文章:

c# - 使用不同类型的节点从 JSON 创建类

c# - 在 MVC 中禁用 html 帮助文本框的自动完成

javascript - 通配符 * 命名组 ( :name*) not working with $routeProvider Angular js v1. 0.6

javascript - Node.js 和带参数的 Express 路由

c# - 如何从 Cell_Leave 事件中获取 DataGridViewCell 的值?

c# - 等同于 Java 中的 C# 锁?

c# - 在空值上左加入 Entity Framework

c# - ASP.NET MVC Bootstrap - 我的 _Layout.cshtml 有什么问题?

c# - 传递给 WebApi 的 JQuery 值始终为空

javascript - 如何在表单提交时向路由发送参数