Admin Routes on Plugin

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.
5 years ago
Hello everyone, I'm working with nopCommerce 4 for a couple of months now and I managed to find solutions for most problems I encountered, but I can't seem to shake this particular problem off.
I created a plugin which add a new menu item on the admin menu, and I can't seem to be able to redirect to a view I created, I always get a "404" error.
My code is below:
Here I declare the menu item.
       
public void ManageSiteMap(SiteMapNode rootNode)
        {
            var menuItem = new SiteMapNode()
            {
                SystemName = "Nop.Plugin.MyPlugin",
                Title = "Plugin title",
                ControllerName = "PluginController",
                ActionName = "Index",
                Visible = true,
                IconClass = "fa-cog",
                RouteValues = new RouteValueDictionary() { { "area", null } },
            };
        
                rootNode.ChildNodes.Add(menuItem);
        }
    }

This is my route provider class
  
  class RouteProvider : IRouteProvider
    {
        public int Priority
        {
            get
            {
                return int.MaxValue;
            }
        }
        public void RegisterRoutes(IRouteBuilder routeBuilder)
        {

            var route = routeBuilder.MapRoute(
                              name: "Admin.Plugin.Index",
                              template: "Admin/Plugin/Index",
                              defaults: new { controller = "PluginController", action = "Index" },
                              dataTokens: new { area = "admin" },
                              constraints: new { }
                );

        }
    }
}

Finally my controller

    class MobijooSettingsController : BasePluginController
    {
        [HttpGet]
        [AuthorizeAdmin]
        [HttpsRequirement(SslRequirement.No)]
        public IActionResult Index() {
            return View("~\\Plugins\\Plugin\\Views\\Index.cshtml");
        }
    }
}

Thank you in advanced
5 years ago
public void ManageSiteMap(SiteMapNode rootNode)
        {
            var menuItem = new SiteMapNode()
            {
                SystemName = "Nop.Plugin.MyPlugin",
                Title = "Plugin title",
                ControllerName = "PluginController",
                ActionName = "Index",
                Visible = true,
                IconClass = "fa-cog",
                RouteValues = new RouteValueDictionary() { { "area", "Admin"} },  // change null this to admin
            };
        
                rootNode.ChildNodes.Add(menuItem);
        }
    }
5 years ago
Hello and thank you for the response.
I've also tried that,as suggested by others, but to no avail.
I managed to "hack" around it creating a configure page for the plugin and overriding "GetConfigurationPageUrl" in my plugin class. Then I just redirected the menu item URL to this page.
It seems to work so far, but I know this is not a proper solution.
Does anyone else has had this happened to them?
5 years ago
hgeorgilas wrote:
Hello everyone, I'm working with nopCommerce 4 for a couple of months now and I managed to find solutions for most problems I encountered, but I can't seem to shake this particular problem off.
I created a plugin which add a new menu item on the admin menu, and I can't seem to be able to redirect to a view I created, I always get a "404" error.
My code is below:
Here I declare the menu item.
       
public void ManageSiteMap(SiteMapNode rootNode)
        {
            var menuItem = new SiteMapNode()
            {
                SystemName = "Nop.Plugin.MyPlugin",
                Title = "Plugin title",
                ControllerName = "PluginController",
                ActionName = "Index",
                Visible = true,
                IconClass = "fa-cog",
                RouteValues = new RouteValueDictionary() { { "area", null } },
            };
        
                rootNode.ChildNodes.Add(menuItem);
        }
    }

This is my route provider class
  
  class RouteProvider : IRouteProvider
    {
        public int Priority
        {
            get
            {
                return int.MaxValue;
            }
        }
        public void RegisterRoutes(IRouteBuilder routeBuilder)
        {

            var route = routeBuilder.MapRoute(
                              name: "Admin.Plugin.Index",
                              template: "Admin/Plugin/Index",
                              defaults: new { controller = "PluginController", action = "Index" },
                              dataTokens: new { area = "admin" },
                              constraints: new { }
                );

        }
    }
}

Finally my controller

    class MobijooSettingsController : BasePluginController
    {
        [HttpGet]
        [AuthorizeAdmin]
        [HttpsRequirement(SslRequirement.No)]
        public IActionResult Index() {
            return View("~\\Plugins\\Plugin\\Views\\Index.cshtml");
        }
    }
}

Thank you in advanced






Hi

In case you haven't figured this out, i'm referring to 3.90 but still relative. The issue is the admin routes are very generic, i.e. admin/{controller}/{action}/{id}. You can see that in the AdminAreaRegistration.cs class. This is called before any plugin registrations so that route is selected first for /admin urls. Setting the priority doesn't work either. You have to put your registration before the default area registration. I've done this and it works fine.

Once you create the route remove it from the collection to prevent a duplicate (throws error), then insert it at the start. But make sure your new admin route is very specific otherwise it could break existing routes.
5 years ago
i'm having exactly the same issue, has anyone tried to find a solution?

i'm adding my route in the routePublisher class but still getting no page found


public virtual void RegisterRoutes(IRouteBuilder routeBuilder)
        {

            routeBuilder.MapLocalizedRoute("AsyncConfiguration", "Admin/Mailchimp/Configuration",
                new { controller = "MailchimpController", action = "Configure" });


            //find route providers provided by other assemblies
            var routeProviders = typeFinder.FindClassesOfType<IRouteProvider>();

            //create and sort instances of route providers
            var instances = routeProviders
                .Where(routeProvider => PluginManager.FindPlugin(routeProvider)?.Installed ?? true) //ignore not installed plugins
                .Select(routeProvider => (IRouteProvider)Activator.CreateInstance(routeProvider))
                .OrderByDescending(routeProvider => routeProvider.Priority);

            //register all provided routes
            foreach (var routeProvider in instances)
                routeProvider.RegisterRoutes(routeBuilder);
        }
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.