【问题标题】:How to control the behavior of Html.ActionLink with MapRoute如何使用 MapRoute 控制 Html.ActionLink 的行为
【发布时间】:2014-12-08 20:44:56
【问题描述】:

在 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+ 模式不匹配,所以服务器去寻找一个不存在的 CommissionPayment 操作。

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

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

【问题讨论】:

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


    【解决方案1】:

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

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

    【讨论】:

    • 哇。就是这样。谢谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-06
    • 1970-01-01
    • 1970-01-01
    • 2012-11-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多