Route Override is not working correctly

1 mês atrás
Hi,

There is very strange behavior in the route override concept. I am using Nop 4.5 version. What I understand is that in the Plugin we can create Route Provider and we can override existing Route Providers which are registered in Nop.Web RouteProvider to redirect to our page.

In my case, I am redirecting to my own plugin page when store is closed.

I override the route in my plugin this way

public partial class RouteProvider : BaseRouteProvider, IRouteProvider
    {
        public void RegisterRoutes(IEndpointRouteBuilder endpointRouteBuilder)
        {
            var lang = GetLanguageRoutePattern();

            //store closed
            endpointRouteBuilder.MapControllerRoute(ComingSoonPageDefaults.Display,
                pattern: $"{lang}/storeclosed",
                defaults: new { controller = "ComingSoonPage", action = "Display" });
        }

        public int Priority
        {
            get { return -1; }
        }
    }


But it always open the Common Route "CommonControll/StoreClosed"

Then for testing purpose what I did is, comment this one in "\Nop.Web\Infrastructure\RouteProvider.cs"

////store closed
            //endpointRouteBuilder.MapControllerRoute(name: "StoreClosed",
            //    pattern: $"{lang}/storeclosed",
            //    defaults: new { controller = "Common", action = "StoreClosed" });


And modify my plugin route provider like this

public partial class RouteProvider : BaseRouteProvider, IRouteProvider
    {
        public void RegisterRoutes(IEndpointRouteBuilder endpointRouteBuilder)
        {
            var lang = GetLanguageRoutePattern();

            //store closed
            endpointRouteBuilder.MapControllerRoute(ComingSoonPageDefaults.Display,
                pattern: $"{lang}/storeclosed",
                defaults: new { controller = "ComingSoonPage", action = "Display" });

            endpointRouteBuilder.MapControllerRoute(name: "StoreClosed",
                pattern: $"{lang}/storeclosed",
                defaults: new { controller = "Common", action = "StoreClosed" });
        }

        public int Priority
        {
            get { return -1; }
        }
    }


This starts showing my page, but when I move the Common Controller route at top, then it opens the Common Controller, That means whatever is registered first, it will show that. Can you any one respond, is this the right behavior?

Regards,
Jamil
1 mês atrás
do you have try after changing priority of your route ?

public int Priority
        {
            get { return -1; }
        }
1 mês atrás
Yes, I tried it already.

I added log entries to verify which is called first. The Nop.Web Route Provider executes first and then My Plugin's one.

But like I said, it shows the view which is registered first. As I mention in my previous post.

I don't know that matters or not but just to point here, I am trying to override the route of Common Controller (i.e: StoreClosed route).
1 mês atrás
you can remove the old registration and push your new registration from plugin
helpful link
1.https://stackoverflow.com/questions/51091247/remove-route-from-routecollection-in-asp-net-core-and-add-new-with-same-route-na
2.https://www.nopcommerce.com/en/boards/topic/41417/how-to-override-shoppingcart-route-without-increase-priority
1 mês atrás
Thanks! for the links.

I your 2nd link, it's 6 years old and I think that is not valid for version 4.5. The first link for stack overflow, it's also posting the old version code but there is one comment in the last thread of that post.

[quote]
The key to the code above is the Priority value. This route will get added to the list first and will therefore take precedence over the default route. Using this technique eliminates the need to delete the default route.

The second possible method turns out to not work because the endpointRouteBuilder.DataSources[n].Endpoints collection is read only. So, as far as I know, you can't remove mappings from that list after they have been added.
[/quote]

In the bold line, he said having priority to 100 will add this to the list first and will take the precedence. That's mean my point is valid.

I just verify, I got this code from a developer in another company, and I notice that they modify the Priority of Route Provider in Nop.Web to int.MaxValue, while in the default code of nop downloaded from nop website it's value is 0.

This verifies that my point was valid and first registered route will take preference.

Thanks! for your input.