Overriding nopCommerce Default Localized Routes in a Plugin

You can create a nopCommerce plugin and you did many. But now you have a need to override the default nopCommerce routes such as product, category, manufacture and so on. Well, if your needs are the same then you are at the right place. Shortly, i will show you can override default nopCommerce localized routes and implement your own functionality using your custom controllers and views.

If you are beginner to nopCommerce plugin development then i suggest you follow nopCommerce developer's guide. This will give you a quick starts guide. I have already created a plugin (as a class library) in nopCommerce project solution with basic folders as shown below;

Project Structure

Next, we need to write code to override the default localized route so that we can point that route to our custom controllers and views. So, below is the class which implements the default nopCommerce provided interface called IRouteProvider and the Periority must be set to the -1 so that it overrides the previous (default) route and not override the default routes for other pages such as login, register and other nopcommerce routes.

    
    public class RouteProvider : IRouteProvider
    {
        public void RegisterRoutes(RouteCollection routes)
        {

        }

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

At this point, we don't any routes, so let's create a couple of routes i.e one for setting (just for demonstration purpose) and one to override the default route.

    public class RouteProvider : IRouteProvider
    {
        private const string NAMESPACES = "Nop.Plugin.Misc.Custom.Controllers";
        private const string CONTROLLER = "MiscCustom";

        public void RegisterRoutes(RouteCollection routes)
        {
            routes.MapRoute("Plugin.Misc.Custom.Settings",
                 "Admin/Plugin/NopWarehouse/Custom/Settings",
                 new { controller = CONTROLLER, action = "Settings" },
                 new[] { NAMESPACES }
            ).DataTokens.Add("area", "admin");

            //Public Override
            routes.MapGenericPathRoute("Plugin.Misc.Custom.GenericUrl",
                "{generic_se_name}",
                new { controller = "Common", action = "GenericUrl" },
                new[] { NAMESPACES });


        }

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

As you can see the MapGenericPathRoute with a custom key provided and our custom controllers namespace (in the plugin0 makes it this route overridable. Now just to prove that this works, i will create a controller called TopicController as this will override the default TopicController. And an action method to override the default topic details view.

    public partial class TopicController : BasePublicController
    {
        public TopicController()
        {

        }

        public ActionResult TopicDetails(int topicId)
        {
            ViewBag.Message = "The TopicDetails view is overriden.";
            return View("~/Plugins/Misc.Custom/Views/Topic/TopicDetails.cshtml");
        }

    }

To show Message in the view;

<h1>@ViewBag.Message</h1>

Now to make this plugin installable, i will create a class which implements IMiscPlugin interface;

    public class CustomPlugin : BasePlugin, IMiscPlugin
    {
        const string NAMESPACE = "Nop.Plugin.Misc.Custom.Controllers";
        public void GetConfigurationRoute(out string actionName, out string controllerName, 
            out System.Web.Routing.RouteValueDictionary routeValues)
        {
            actionName = null;
            controllerName = null;
            routeValues = new RouteValueDictionary()
            {
                { "Namespaces", NAMESPACE }, 
                { "area", null }
            };

        }
    }

Now, let's rebuild the solution and go to nopCommerce administration and install the plugin;

Now, let's click on the about us page and see the results. In my case the local URL is http://localhost:15536/about-us.

And there you can see the results :)

So, as you can see its very easy to override the default localized route and think for a second how much power it gives you in a plugin if in case you need to write and display your own views.

Source Code

https://copy.com/PkxvgLVVLhnnTQz4




Write your own review
Bad Excellent