aspnet / Mvc

[Archived] ASP.NET Core MVC is a model view controller framework for building dynamic web sites with clean separation of concerns, including the merged MVC, Web API, and Web Pages w/ Razor. Project moved to https://github.com/aspnet/AspNetCore
Apache License 2.0
5.62k stars 2.14k forks source link

Link generation with conventional routes is broken with endpoint routing #8613

Closed pranavkm closed 5 years ago

pranavkm commented 5 years ago

Title needs tweaking, but here's the gist of it:

// Startup:
app.UseMvc(routes =>
{
    routes.MapRoute(
        name: "default",
        template: "{controller=Home}/{action=Index}/{id?}");
});

// Controller
public class HomeController : Controller
{
    public IActionResult Index(int id)
    {
        return View();
    }
}

// Index.cshtml
<a asp-route-id="3">Link to item 3</a>

Expected: (also what I get with 2.1 routing): /Home/Index/3 Actual: /?id=3

pranavkm commented 5 years ago

cc @rynowak \ @JamesNK

This one seems icky

rynowak commented 5 years ago

@JamesNK - it looks like what's happening here is that the longer route isn't getting prioritized. We should look at the precedence of the routes that are decomposed from the conventional route when defaults are in play.

JamesNK commented 5 years ago

I don't think the longer route should always be prioritized. I think the issue might be in the link generator. If there is a route value for an optional parameter then the link generator should choose prioritize the endpoint with the parameter.

<a asp-route-id="3">Link to item 3</a> /Home/Index/3 - we want the longer route because of the optional parameter with route value

<a asp-route-action="Index">Link to item index</a> / - want the shorter route

<a asp-route-custom="Custom">Link to item custom</a> /?custom=Custom - want the shorter route