Override Existing Controller & Action in Nop Version 4.0

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.
6 anni tempo fa
Hi every body.

I want to Override Customer Controller (Register action inside that) in nop version 4.
please advice me, if possible.
My problem is more in ViewEngine and RouteProvider.
[Best Regards]
Thanks
6 anni tempo fa
Hamidnch wrote:
Hi every body.

I want to Override Customer Controller (Register action inside that) in nop version 4.
please advice me, if possible.
My problem is more in ViewEngine and RouteProvider.
[Best Regards]
Thanks


Like any other version, you need to inherit from CustomerController and override the method. You also need to register your new class so .net core can find, instantiate and inject the correct class.
6 anni tempo fa
Hi.Thanks for your reply.
My problem is Override route and view.
For example, What's alternative for these:

For Route:

            var productCreateRoute =
                routes.MapRoute("Nop.Plugin.Misc.Sample.ProductCreate",
                    "Admin/Product/Create",
                    new { controller = "CustomProductAdmin", action = "CustomCreate", area = "Admin" },
                    new[] { "Nop.Plugin.Misc.Sample.Controllers" });
            productCreateRoute.DataTokens.Add("area", "admin");

            routes.Remove(productCreateRoute);
            routes.Insert(0, productCreateRoute);


For ViewEngine:

    public class CustomViewEngine : ThemeableRazorViewEngine
    {
ViewLocationFormats = new[]
                {
                    "~/Plugins/Misc.Sample/Views/{1}/{0}.cshtml",
                    "~/Plugins/Misc.Sample/Views/{0}.cshtml"
                };
                PartialViewLocationFormats = new[]
                {
                    "~/Plugins/Misc.Sample/Views/{1}/{0}.cshtml",
                    "~/Plugins/Misc.Sample/Views/{0}.cshtml"
                };
                AreaViewLocationFormats = new[]
                {
                    "~/Plugins/Misc.Sample/Views/{1}/{0}.cshtml",
                    "~/Plugins/Misc.Sample/Views/{0}.cshtml"
                };
                AreaPartialViewLocationFormats = new[]
                {
                    "~/Plugins/Misc.Sample/Views/{1}/{0}.cshtml",
                    "~/Plugins/Misc.Sample/Views/{0}.cshtml"
                };
    }


Thanks
[Best Regards]
6 anni tempo fa
Hamidnch wrote:
Hi.Thanks for your reply.
My problem is Override route and view.
For example, What's alternative for these:

For Route:

            var productCreateRoute =
                routes.MapRoute("Nop.Plugin.Misc.Sample.ProductCreate",
                    "Admin/Product/Create",
                    new { controller = "CustomProductAdmin", action = "CustomCreate", area = "Admin" },
                    new[] { "Nop.Plugin.Misc.Sample.Controllers" });
            productCreateRoute.DataTokens.Add("area", "admin");

            routes.Remove(productCreateRoute);
            routes.Insert(0, productCreateRoute);


[Best Regards]


For route you can use simple routing like


public partial class RouteProvider : IRouteProvider
    {
        /// <summary>
        /// Register routes
        /// </summary>
        /// <param name="routeBuilder">Route builder</param>
        public void RegisterRoutes(IRouteBuilder routeBuilder)
        {
            routeBuilder.MapRoute("Plugin.Payments.PayPalDirect.Webhook", "Plugins/PaymentPayPalDirect/Webhook/",
                new { controller = "PaymentPayPalDirect", action = "WebhookEventsHandler" });

        }

        /// <summary>
        /// Gets a priority of route provider
        /// </summary>
        public int Priority
        {
            get { return -1; }
        }
    }


You refer any payment plugin, it'll work.
6 anni tempo fa
Hamidnch wrote:

For ViewEngine:

    public class CustomViewEngine : ThemeableRazorViewEngine
    {
ViewLocationFormats = new[]
                {
                    "~/Plugins/Misc.Sample/Views/{1}/{0}.cshtml",
                    "~/Plugins/Misc.Sample/Views/{0}.cshtml"
                };
                PartialViewLocationFormats = new[]
                {
                    "~/Plugins/Misc.Sample/Views/{1}/{0}.cshtml",
                    "~/Plugins/Misc.Sample/Views/{0}.cshtml"
                };
                AreaViewLocationFormats = new[]
                {
                    "~/Plugins/Misc.Sample/Views/{1}/{0}.cshtml",
                    "~/Plugins/Misc.Sample/Views/{0}.cshtml"
                };
                AreaPartialViewLocationFormats = new[]
                {
                    "~/Plugins/Misc.Sample/Views/{1}/{0}.cshtml",
                    "~/Plugins/Misc.Sample/Views/{0}.cshtml"
                };
    }



For TheamViewEngine you need to use "ExpandViewLocations" in Core.

Inherit your themeEngine by "IViewLocationExpander"


public class CustomViewLocationExpander : IViewLocationExpander
    {
        private const string THEME_KEY = "nop.themename";
      
        public void PopulateValues(ViewLocationExpanderContext context)
        {
            //no need to add the themeable view locations at all as the administration should not be themeable anyway
            if (context.AreaName?.Equals(AreaNames.Admin) ?? false)
                return;

            var themeContext = (IThemeContext)context.ActionContext.HttpContext.RequestServices.GetService(typeof(IThemeContext));
            context.Values[THEME_KEY] = themeContext.WorkingThemeName;
        }
      
        public IEnumerable<string> ExpandViewLocations(ViewLocationExpanderContext context, IEnumerable<string> viewLocations)
        {
            if (context.Values.TryGetValue(THEME_KEY, out string theme))
            {
                viewLocations = new[] {
                        $"/Themes/{theme}/Views/{{1}}/{{0}}.cshtml",
                        $"/Themes/{theme}/Views/Shared/{{0}}.cshtml",

                         $"~/Plugins/Misc.Sample/Views/{{1}}/{{0}}.cshtml",
                         $"~/Plugins/Misc.Sample/Views/{{0}}.cshtml"    
                    }
                    .Concat(viewLocations);
            }
            
            return viewLocations;
        }
    }

6 anni tempo fa
And inherit your "DependencyRegistrar.cs" file with INopStartup

and register your CustomViewLocationExpander.




public void ConfigureServices(IServiceCollection services, IConfigurationRoot configuration)
{
           //themes support
            services.Configure<RazorViewEngineOptions>(options =>
            {
                options.ViewLocationExpanders.Add(new CustomViewLocationExpander());
            });
}
6 anni tempo fa
Hi.
Thanks for your reply. It was a very comprehensive and complete.
[Best regards]
6 anni tempo fa
rajupaladiya wrote:
Hi.Thanks for your reply.
My problem is Override route and view.
For example, What's alternative for these:

For Route:

            var productCreateRoute =
                routes.MapRoute("Nop.Plugin.Misc.Sample.ProductCreate",
                    "Admin/Product/Create",
                    new { controller = "CustomProductAdmin", action = "CustomCreate", area = "Admin" },
                    new[] { "Nop.Plugin.Misc.Sample.Controllers" });
            productCreateRoute.DataTokens.Add("area", "admin");

            routes.Remove(productCreateRoute);
            routes.Insert(0, productCreateRoute);


[Best Regards]

For route you can use simple routing like


public partial class RouteProvider : IRouteProvider
    {
        /// <summary>
        /// Register routes
        /// </summary>
        /// <param name="routeBuilder">Route builder</param>
        public void RegisterRoutes(IRouteBuilder routeBuilder)
        {
            routeBuilder.MapRoute("Plugin.Payments.PayPalDirect.Webhook", "Plugins/PaymentPayPalDirect/Webhook/",
                new { controller = "PaymentPayPalDirect", action = "WebhookEventsHandler" });

        }

        /// <summary>
        /// Gets a priority of route provider
        /// </summary>
        public int Priority
        {
            get { return -1; }
        }
    }


You refer any payment plugin, it'll work.


Hi again.

I wrote this:
  

  public class RouteProvider : IRouteProvider
    {
        #region Implementation of IRouteProvider

        public void RegisterRoutes(IRouteBuilder routeBuilder)
        {
            routeBuilder.MapLocalizedRoute("Nop.Plugin.Misc.Sample.HomeAdmin", "Admin/Home/Index", new
            {
                controller = "CustomHomeAdmin",
                action = "Index"
            });
        }

        public int Priority => -1;

        #endregion
    }


But,Can not worked.

Thanks.
6 anni tempo fa
Hamidnch wrote:



I wrote this:
  

  public class RouteProvider : IRouteProvider
    {
        #region Implementation of IRouteProvider

        public void RegisterRoutes(IRouteBuilder routeBuilder)
        {
            routeBuilder.MapLocalizedRoute("Nop.Plugin.Misc.Sample.HomeAdmin", "Admin/Home/Index", new
            {
                controller = "CustomHomeAdmin",
                action = "Index"
            });
        }

        public int Priority => -1;

        #endregion
    }


But,Can not worked.


Hello,

For admin route you need to use Map.AreaRoute.

Use like this


routeBuilder.MapAreaRoute(name: "Nop.Plugin.Misc.Sample.HomeAdmin",
                    template: "Admin/Home/Index",
                    areaName: "Admin",
                    defaults: new { controller = "CustomHomeAdmin", action = "Index" }
                );




This will help you :)
6 anni tempo fa
rajupaladiya wrote:



I wrote this:
  

  public class RouteProvider : IRouteProvider
    {
        #region Implementation of IRouteProvider

        public void RegisterRoutes(IRouteBuilder routeBuilder)
        {
            routeBuilder.MapLocalizedRoute("Nop.Plugin.Misc.Sample.HomeAdmin", "Admin/Home/Index", new
            {
                controller = "CustomHomeAdmin",
                action = "Index"
            });
        }

        public int Priority => -1;

        #endregion
    }


But,Can not worked.


Hello,

For admin route you need to use Map.AreaRoute.

Use like this


routeBuilder.MapAreaRoute(name: "Nop.Plugin.Misc.Sample.HomeAdmin",
                    template: "Admin/Home/Index",
                    areaName: "Admin",
                    defaults: new { controller = "CustomHomeAdmin", action = "Index" }
                );




This will help you :)


Hi dear.
Thanks for your reply.
I used this route:

            routeBuilder.MapAreaRoute(name: "Nop.Plugin.Misc.Sample.HomeAdmin",
                                       template: "Admin/Home/Index",
                                       areaName: "Admin",
                                       defaults: new
                                       {
                                           controller = "CustomHomeAdmin",
                                           action = "CustomIndex"
                                       });


But this method did not work!
I set break point,but execute original Home Controller in Admin area.
Does it have other tips? I have other work to do?

Thanks a lot.
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.