Routing - cannot change precedence of DynamicRouteValueTransformer
See original GitHub issueIs there an existing issue for this?
- I have searched the existing issues
Describe the bug
This is related to https://github.com/dotnet/aspnetcore/issues/29594
For our applications, we developed a CMS which implies that most of our routes are dynamic and determined during runtime - users can create and publish pages. For this I created a custom DynamicRouteValueTransformer which would check if a page for a given url exists in the database. Typically we would configure the routing in our applications like this:
app.UseEndpoints(endpoints =>
{
endpoints.MapDynamicControllerRoute<CmsRoute>("{**slug}");
endpoints.MapDefaultControllerRoute();
});
This worked like a charm, in .NET 5.0. If we had a route configured using attribute routing, that route would match and the CmsRoute would not be invoked. I believe since updating to .NET 6.0, the DynamicRouteValueTransformer is always invoked, just as described in the #29594. I am not sure about the solution though.
Mapping to a fallback controller doesn’t work, since we have defined custom Controllers for certain page types (a page of type Foobar in the Cms is eventually mapped, in the DynamicRouteValueTransformer, to the FoobarController by setting the correct values in the RouteValueDictionary). I would somehow need to invoke a FoobarController from the FallbackController, which I am not even sure is possible (a redirect obviously is not desirable).
I came across https://github.com/dotnet/aspnetcore/issues/34316 which mentions using the ApplicationModelProvider. While I haven’t researched it yet, it sounds like quite some refactoring to make it working again. Moreover, I am concerned about performance issues, since pages may be created at runtime, can be scheduled to go live at certain points in time, so it sounds like I would have to invoke the IChangeToken quite often (e.g. every 10 seconds, which I am not sure is desirable).
I was actually hoping I am missing a small change I can make to make the DynamicRouteValueTransformer working again so it is not invoking for endpoints configured using conventional routes. Also, after reading a lot about the routing I still don’t understand why a catch-all route like {**slug} is considered to have more precedence than a controller endpoint with a [Route("/xyz")] defined as endpoint.
.NET Version
6.0+
Issue Analytics
- State:
- Created 10 months ago
- Comments:10 (6 by maintainers)

Top Related StackOverflow Question
@razzemans did you set the order for your matcher policy? It needs to be very low, like
int.MinValue + 10so that it runs before the one for dynamic controllers. https://github.com/dotnet/aspnetcore/blob/main/src/Mvc/Mvc.Core/src/Routing/DynamicControllerEndpointMatcherPolicy.cs#L35@javiercn Big thank you! Setting it really low works. This is a much nicer solution, so thanks again for your time!