Use ID in Plugin Custom Route

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.
1 year ago
So I'm aware Nop is id-less routing. But I've made a plugin where I want to route using the ID.

 public partial class RouteProvider : BaseRouteProvider, IRouteProvider
    {
      
        public int Priority => 10;

        public void RegisterRoutes(IEndpointRouteBuilder endpointRouteBuilder)
        {          
                var lang = GetLanguageRoutePattern();

                endpointRouteBuilder.MapControllerRoute(name: "Fits",
                  pattern: "{lang}/{controller=Fits}/{action=Index}/{id?}");

        }
    }



The controller:


public async Task<IActionResult> Index(int id) {}


This route works perfectly  as www.foo.com/controller/?id=1

But anything else:
www.foo.com/controller/1
www.foo.com/controller/index/1

will hit the controller with an id == 0.

Controller loads, it loads the view, then rewrites the url to page-not-found and shows "page not found"

If in the debugger I manually change id from 0 to 1, the url still gets re-written to page-not-found but the plugin view loads as expected with the correct data.

It seems my issue is directly tied to Nop's id-less routing. Basically: How do I circumvent this in a plugin?
1 year ago
What nopCommerce version?
1 year ago
4.60
1 year ago
Solved it using alternate notation for the route:


        public void RegisterRoutes(IEndpointRouteBuilder endpointRouteBuilder)
        {
            //If the plugin is enabled we overwrite the route to use our view instead.

            var lang = GetLanguageRoutePattern();

            endpointRouteBuilder.MapControllerRoute(name: "Foo",
                pattern: $"{lang}/foo/{{id?}}",
                defaults: new { controller = "Foo", action = "Index" });

        }
1 year ago
FYI, there's this similar example in the Avalara Tax plugin

 endpointRouteBuilder.MapControllerRoute(name: AvalaraTaxDefaults.DownloadCertificateRouteName,
                pattern: "download-tax-exemption-certificate/{id:min(0)}",
                defaults: new { controller = "AvalaraPublic", action = "DownloadCertificate" });
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.