Search code examples
asp.netasp.net-mvcasp.net-mvc-5visual-studio-2015attributerouting

ActionLink generates wrong URL for Attribute Route


I'm using attribute routing to override the URLs in my controller. I have two actions, with the only difference being the ID present on the second. The rest of the parameters are optional query parameters used for searching.

// RouteConfig.cs - I setup AttributeRoutes before any other mapped routes.
routes.MapMvcAttributeRoutes();

// Controllers/DeliveryController.cs
[Route("mvc/delivery")]
public ActionResult Delivery(string hauler, DateTime? startDate, DateTime? endDate, int? page)
{
    // ...
    return View(model);
}

[Route("mvc/delivery/{id}")]
public ActionResult Delivery(int id, string hauler, DateTime? startDate, DateTime? endDate, int? page)
{
    // ...
    return View("DeliverySelected", model);
}

Both routes work as expected when manually navigating to /mvc/delivery and /mvc/delivery/1234/, however links are generating incorrectly.

@Html.ActionLink("Delivery", "Delivery", new { id = delivery.ID })
@Url.Action("Delivery", new { id = delivery.ID })

Either method generates links like the following, which triggers the first action instead of the second:

http://localhost:53274/mvc/delivery?id=1234

How can I generate the expected URL instead?

http://localhost:53274/mvc/delivery/1234

Solution

  • I found the answer, thanks to this answer concerning ambiguous action methods. You can only have a maximum of 2 action methods with the same name in a controller.

    I did have a third method in this case, that I left out since I thought it was unrelated:

    [HttpPost]
    [Route("mvc/delivery")]
    public ActionResult Delivery(DeliveryViewModel model)
    

    Renaming my second action to SelectDelivery(int id, /*...*/) solved the issue.