Skip to content

Dynamic routing in ASP.NET Core 3.1 when part of the url is already another route #18688

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
jsabrooke opened this issue Jan 30, 2020 · 5 comments
Assignees
Labels
area-mvc Includes: MVC, Actions and Controllers, Localization, CORS, most templates investigate
Milestone

Comments

@jsabrooke
Copy link

Using the code:

app.UseEndpoints(endpoints =>
{
    var defaults = new { controller = "Home", action = "Index" };
    endpoints.MapControllerRoute(name: "test", pattern: "category/product1", defaults: defaults);
    endpoints.MapDynamicControllerRoute<RouteValueTransformer>("{**slug}");
});

I would expect:

  • "category/product2" to attempt to find a route by entering the RouteValueTransformer.TransformAsync method. However, it doesn't, which appears to be because the first part, "category", is matched. It then gives a 404 as there isn't actually a route for it.

If you change "category" to anything else, it will enter the TransformAsync method as expected, which is why I think it's just matching the first "category" part of the url.

I'm not sure if this is expected behaviour, a bug, or if we're using the "pattern" parameter wrong, or maybe {**slug} wrong?

I made a Stack Overflow post for this as well with a bit more detail, but I think the above is enough for here.

https://stackoverflow.com/questions/59971600/dynamic-routing-in-asp-net-core-3-1-when-part-of-the-url-is-already-another-rout

It's a different problem, but maybe the same cause as #16579 ?

Thanks very much!

To Reproduce

It's very easy to reproduce. Create a new ASP.NET Core 3.1 web application using the template in VS 2019. Add the code above to Startup.Configure.

Add

services.AddSingleton<RouteValueTransformer>();

to Startup.ConfigureServices.

Add

public class RouteValueTransformer : DynamicRouteValueTransformer
{
    public override async ValueTask<RouteValueDictionary> TransformAsync(HttpContext httpContext, RouteValueDictionary values)
    {
        return values;
    }
}

Then put a breakpoint on "return values;", and see when it gets hit by different urls.

Further technical details

  • ASP.NET Core 3.1.0
  • VS 2019 Professional 16.4.3
  • Include the output of dotnet --info:

.NET Core SDK (reflecting any global.json):
Version: 3.1.101
Commit: b377529961

Runtime Environment:
OS Name: Windows
OS Version: 10.0.18363
OS Platform: Windows
RID: win10-x64
Base Path: C:\Program Files\dotnet\sdk\3.1.101\

Host (useful for support):
Version: 3.1.1
Commit: a1388f194c

.NET Core SDKs installed:
1.1.14 [C:\Program Files\dotnet\sdk]
2.1.202 [C:\Program Files\dotnet\sdk]
2.1.509 [C:\Program Files\dotnet\sdk]
2.2.100 [C:\Program Files\dotnet\sdk]
2.2.207 [C:\Program Files\dotnet\sdk]
2.2.401 [C:\Program Files\dotnet\sdk]
3.1.100 [C:\Program Files\dotnet\sdk]
3.1.101 [C:\Program Files\dotnet\sdk]

.NET Core runtimes installed:
Microsoft.AspNetCore.All 2.1.13 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
Microsoft.AspNetCore.All 2.1.14 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
Microsoft.AspNetCore.All 2.1.15 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
Microsoft.AspNetCore.All 2.2.0 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
Microsoft.AspNetCore.All 2.2.6 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
Microsoft.AspNetCore.All 2.2.8 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
Microsoft.AspNetCore.App 2.1.13 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 2.1.14 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 2.1.15 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 2.2.0 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 2.2.6 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 2.2.8 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 3.1.0 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 3.1.1 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.NETCore.App 1.0.16 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 1.1.13 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 2.0.9 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 2.1.13 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 2.1.14 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 2.1.15 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 2.2.0 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 2.2.6 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 2.2.8 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 3.1.0 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 3.1.1 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.WindowsDesktop.App 3.1.0 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
Microsoft.WindowsDesktop.App 3.1.1 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]

@pranavkm pranavkm added the area-mvc Includes: MVC, Actions and Controllers, Localization, CORS, most templates label Jan 30, 2020
@mkArtakMSFT mkArtakMSFT added this to the Next sprint planning milestone Jan 30, 2020
@mkArtakMSFT
Copy link
Contributor

Thanks for contacting us, @jsabrooke.
@javiercn can you please look into this? Thanks!

@douwinga
Copy link

douwinga commented Mar 2, 2020

Ran into this exact issue today too. As a workaround I found that using attribute routing works instead of using MapControllerRoute.

@jsabrooke
Copy link
Author

Thanks a lot @douwinga ! I'll try that while waiting for a resolution on this.

@ygoe
Copy link

ygoe commented May 25, 2020

I have no idea what "attribute routing" should look like in this scenario. (Or in mine, see last referenced issue.)

@jsabrooke
Copy link
Author

jsabrooke commented May 28, 2020

Closing as I've just discovered that it's a duplicate of #18677, which was created about half a day before mine was, and a fix is planned:

https://docs.microsoft.com/en-us/aspnet/core/mvc/controllers/routing?view=aspnetcore-3.1#conventional-routing-order

@ghost ghost locked as resolved and limited conversation to collaborators Jun 27, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-mvc Includes: MVC, Actions and Controllers, Localization, CORS, most templates investigate
Projects
None yet
Development

No branches or pull requests

6 participants