How to control the behavior of Html.ActionLink with MapRoute

101 Views Asked by At

In MSVC4, what is the accepted method of generating links in the form /Controller/Action/parameter?

I have this in an .ascx...

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

...which returns this:

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

I have this in RouteConfig.vb

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

...and this URL correctly invokes the GetOBDocument method on the GetDocument controller:

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

However, the URL with "?DocID=123" returned by the ActionLink call invokes nothing. It's invalid garbage; "The resource cannot be found". I suppose that's because it doesn't match the /\d+ pattern for the CommissionPayment route, so the server goes looking for a CommissionPayment action, which doesn't exist.

Clearly, I can either leave out the maproute thing and use ?DocID, or I can write the URL by hand. Probably the latter, since users download the files and that form fools the browser into letting me control the filename it's downloaded to.

But I'd like to understand what's going on here.

1

There are 1 best solutions below

0
On BEST ANSWER

In your route definition the action is defined as GetOBDocument so when using Html.ActionLink you should provide that as the action name instead of CommissionPayment:

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