Plugin to overrides the default controller

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.
10 years ago
Hi everybody,

I am facing an issues, the condition as below:-

1. I have build a plugin that to enhance on customer details with new attributes such as account number, upline id and etc.
2. I want to override the default controller with the controller found in the plugin instead of making modification on original source such as CustomerController
3. I also want to override the default controller at admin side

The purpose of doing this is because to avoid the difficulty of upgrade. Therefore, I choose to build it as plugin.

But then I try to register the routes in plugin, I found that the routeprovider is executed first then only the default the code as below:
public void RegisterRoutes(RouteCollection routes)
        {
            
            var route = routes.MapLocalizedRoute("Register",
                 "register",
                 new { controller = "Customer", action = "Register" },
                 new[] { "Nop.Plugin.Misc.Enhancement.Controllers" }
            );

            routes.Remove(route);
            routes.Insert(0, route);
        }

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


but then i found that the route in the plugin was replaced by the default

is there anyway to stop it or is there anyway to hit the conditions as i mention above
10 years ago
I have a method from this post https://www.nopcommerce.com/boards/t/26201/modifying-built-in-controller.aspx

But it is a trick. I wish place the view like default but with custom model and the controller is using the custom controller i created in plugin instead of default.

Is it able to make it happen?
10 years ago
etws14 wrote:
Hi everybody,

I am facing an issues, the condition as below:-

1. I have build a plugin that to enhance on customer details with new attributes such as account number, upline id and etc.
2. I want to override the default controller with the controller found in the plugin instead of making modification on original source such as CustomerController
3. I also want to override the default controller at admin side

The purpose of doing this is because to avoid the difficulty of upgrade. Therefore, I choose to build it as plugin.

But then I try to register the routes in plugin, I found that the routeprovider is executed first then only the default the code as below:
public void RegisterRoutes(RouteCollection routes)
        {
            
            var route = routes.MapLocalizedRoute("Register",
                 "register",
                 new { controller = "Customer", action = "Register" },
                 new[] { "Nop.Plugin.Misc.Enhancement.Controllers" }
            );

            routes.Remove(route);
            routes.Insert(0, route);
        }

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


but then i found that the route in the plugin was replaced by the default

is there anyway to stop it or is there anyway to hit the conditions as i mention above


Priority should be positive to override the default route.
10 years ago
I have tried with positive as well, but still overwrite by default route too
10 years ago
It show me "A route named 'Register' is already in the route collection. Route names must be unique" so I think need a way to remove the default then we only can make it work
10 years ago
etws14 wrote:
I have tried with positive as well, but still overwrite by default route too


Try a bigger number.

Also you don't need to remove the original one, just override with your custom route is fine.

            routes.MapLocalizedRoute("MyRegister",
                            "register/",
                            new { controller = "Customer", action = "Register" },
                            new[] { "Nop.Web.Controllers.Custom.whatever" });
10 years ago
Okay I have fixed the issues.


public partial class RouteProvider : IRouteProvider
    {
        public void RegisterRoutes(RouteCollection routes)
        {
            ViewEngines.Engines.Add(new CustomViewEngine());

            var route = routes.MapLocalizedRoute("Register2",
                 "register",
                 new { controller = "Customer", action = "Register" },
                 new[] { "Nop.Plugin.Misc.Enhancement.Controllers" }
            );
        }

        public int Priority
        {
            get { return 100; }
        }
    }
10 years ago
admin controller works slightly differently, you can find more details here.

http://tech.sunnyw.net/2014/01/nopcommerce-register-your-customized.html
8 years ago
etws14 wrote:
Okay I have fixed the issues.


public partial class RouteProvider : IRouteProvider
    {
        public void RegisterRoutes(RouteCollection routes)
        {
            ViewEngines.Engines.Add(new CustomViewEngine());

            var route = routes.MapLocalizedRoute("Register2",
                 "register",
                 new { controller = "Customer", action = "Register" },
                 new[] { "Nop.Plugin.Misc.Enhancement.Controllers" }
            );
        }

        public int Priority
        {
            get { return 100; }
        }
    }


Hi etws14,

How did you override the controller?

Thanks
8 years ago
etws14 wrote:
Okay I have fixed the issues.


public partial class RouteProvider : IRouteProvider
    {
        public void RegisterRoutes(RouteCollection routes)
        {
            ViewEngines.Engines.Add(new CustomViewEngine());

            var route = routes.MapLocalizedRoute("Register2",
                 "register",
                 new { controller = "Customer", action = "Register" },
                 new[] { "Nop.Plugin.Misc.Enhancement.Controllers" }
            );
        }

        public int Priority
        {
            get { return 100; }
        }
    }


But this will overrite entire Customer controller. What if you want to only override only Register action? is it possible?
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.