question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Routing - cannot change precedence of DynamicRouteValueTransformer

See original GitHub issue

Is 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:closed
  • Created 10 months ago
  • Comments:10 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
javiercncommented, Nov 24, 2022

@razzemans did you set the order for your matcher policy? It needs to be very low, like int.MinValue + 10 so 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

0reactions
razzemanscommented, Nov 24, 2022

@javiercn Big thank you! Setting it really low works. This is a much nicer solution, so thanks again for your time!

Read more comments on GitHub >

github_iconTop Results From Across the Web

MVC Routing: route precedence not working as expected
I study a MVC sample I have a controller Home and a action Index. I have the following routing registration: public static void...
Read more >
Routing Precedence not working as expected - Discussions
Hi, I am currently changing our IPSEC VPNs from Cisco ASA to Sophos XGS, ... Route-precedence is VPN-Static-SD-WAN.
Read more >
Routing in ASP.NET Core
Route template precedence: Avoids the need to adjust the order of endpoints in common cases. Attempts to match the common-sense expectations of ...
Read more >
Routing to controller actions in ASP.NET Core
Matches from a route that appears earlier have a higher priority. Conventional routing is order-dependent. In general, routes with areas should ...
Read more >
How do I configure route precedence on a firewall?
Go to the Manage tab · Click Network | Routing · Modify or Add a static route · Click on the Advanced tab...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found