如何使用MapRoute控制Html.ActionLink的行为

时间:2014-12-08 20:44:56

标签: vb.net asp.net-mvc-4 html.actionlink maproute

在MSVC4中,以/Controller/Action/parameter形式生成链接的可接受方法是什么?

我在.ascx中有这个...

<%=Html.ActionLink(linkText:=doc.DocumentName, _
                  actionName:="CommissionPayment", _
                  controllerName:="GetDocument", _
                  routeValues:=New With {.DocID = doc.DocumentID}, _
                  htmlAttributes:=Nothing)
                  %>

...返回此信息:

http://localhost:56869/GetDocument/CommissionPayment?DocID=5511972

我在RouteConfig.vb中有这个

routes.MapRoute(
    "CommissionPayment", _
    "GetDocument/CommissionPayment/{DocID}", _
    New With {.controller = "GetDocument", .action = "GetOBDocument"}, _
    New With {.DocID = "\d+"} _
    )

...此URL正确调用GetDocument控制器上的GetOBDocument方法:

http://localhost:56869/GetDocument/CommissionPayment/123123123

但是,ActionLink调用返回的带有“?DocID = 123”的URL不会调用任何内容。这是无效的垃圾; “没有找到您要查的资源”。我想那是因为它与CommissionPayment路由的/ \ d +模式不匹配,所以服务器会寻找佣金支付行动,而这种行为并不存在。

显然,我可以省略maproute并使用?DocID,或者我可以手动编写URL。可能是后者,因为用户下载文件并且这种形式欺骗了浏览器让我控制它下载的文件名。

但我想了解这里发生了什么。

1 个答案:

答案 0 :(得分:1)

在您的路线定义中,操作定义为GetOBDocument,因此在使用Html.ActionLink时,您应该将其作为操作名称而不是CommissionPayment

<%=Html.ActionLink(linkText:=doc.DocumentName, _
              actionName:="GetOBDocument", _
              controllerName:="GetDocument", _
              routeValues:=New With {.DocID = doc.DocumentID}, _
              htmlAttributes:=Nothing)
              %>