Override admin controller action

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.
7 years ago
So I am trying to add additional functionality where you add/create a topic in admin.

I am basically inheriting the "TopicController" in Nop.Admin.Controllers

Then I am trying to override route (which I can easily do in the public namespace). I am trying to do like this:

routes.MapLocalizedRoute("TopicAdditionalInfoEdit",
              "Topic/Edit/{id}",              
              new { controller = controllerName, action = "Edit", Area = "Admin"},
              new { id = @"\d+" },
              new[] { nameSpace });

    

Where controller and namespace obviously goes to my plugin instead.

Then I override the base class:

   public override ActionResult Edit(int id)
        {
            return base.Edit(id);
        }
        public override ActionResult Edit(Nop.Admin.Models.Topics.TopicModel model, bool continueEditing)
        {
            return base.Edit(model, continueEditing);
        }


Is this possible in any way?

Also - Am I going for a wrong path?. I could create an admin tab etc. But my issue is primarely when creating a topic I do not have any ID.

PS: thanks for a GREAT time on nopcommerce days 2016
7 years ago
Use AcitonFilter to simulate the override. https://www.pronopcommerce.com/overriding-intercepting-nopcommerce-controllers-and-actions
7 years ago
Thanks alot!

Was not really thinking of that possibility. Great to get reminded that this exists :).

I will, however, not use it in this instance (I have other stuff that I want to use it in, that is less complex)

instead I did manage to make my custom routeprovider:


public class CustomViewEngine : ThemeableRazorViewEngine
    {
        public CustomViewEngine()
        {
            List<string> viewPaths = new List<string>();

            viewPaths.Add("~/Administration/Views/Topic/{0}.cshtml");

            ViewLocationFormats = viewPaths.ToArray();

            List<string> partialViewPaths = new List<string>();
            partialViewPaths.Add("~/Administration/Views/Topic/{0}.cshtml");

            PartialViewLocationFormats = partialViewPaths.ToArray();
        }
    }
    public class RouteProvider : IRouteProvider
    {

        public void RegisterRoutes(RouteCollection routes)
        {
            string controllerName = nameof(TopicTemplateController).Replace("Controller", "");
            string nameSpace = typeof(TopicTemplateController).Namespace;

            var routeTopicEdit = routes.MapRoute("Admin.Topic.Edit.AdditionalInfo",
                           "Admin/Topic/Edit/{id}",
                           new { controller = controllerName, action = "Edit", id = UrlParameter.Optional, area = "Admin" },
                           new[] { nameSpace });
            routes.Remove(routeTopicEdit);
            routes.Insert(0, routeTopicEdit);
            
          
            ViewEngines.Engines.Insert(0, new CustomViewEngine());
        }

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


So 2 things happening here:

1) I have to add a view engine. This is because all controller call in admin/core only calls view without path. This means that nopcommerce will have to have added this path to known paths. Otherwise It will use the path of my plugin (I want to reuse the existing views as much as possible)

2) Register my new route that overrides the existing edit route for topics. This ensures that my route will be hit first. Important thing is the area = "Admin" - otherwise nopcommerce will use the base root when looking at controllers - so it will use CommenController outside of admin (not desired)
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.