Optimus
Optimus

Reputation: 2210

How to override route in a plugin nopcommerce

I've a route like Admin/Vendor in my MVC application . Without changing this route I need to point this same route to another method say CustomAdmin/CustomVendor.

I tried attribute routing but no luck . Is there any way to do this. My current code is given below

Original Method:

public class AdminController
{
   public ActionResult Vendor()
    {
        return View();
    }
}

Custom Method:

public class CustomAdminController
{
    [Route("Admin/Vendor")]
    public ActionResult CustomVendor()
    {
        return View();
    }
}

Upvotes: 3

Views: 1760

Answers (2)

Divyang Desai
Divyang Desai

Reputation: 7864

As you're developing a plugin. You have to add your custom route to the RouteProvider.

In default nopCommerce AdminController and Vendor doesn't exists, so I assume that you're trying to override vendor list method of admin.

enter image description here

Which looks like:

public partial class RouteProvider : IRouteProvider
{
    public void RegisterRoutes(RouteCollection routes)
    { 
        var route = routes.MapRoute("Plugin.GroupName.PluginName.CustomVendor",
                                     "Admin/Vendor/List",
                                      new { controller = "CustomAdminController", action = "CustomVendor", orderIds = UrlParameter.Optional, area = "Admin" },
                                      new[] { "Nop.Plugin.GroupName.PluginName.Controllers" });
        route.DataTokens.Add("area", "admin");
        routes.Remove(route);
        routes.Insert(0, route);
    }
    public int Priority
    {
        get
        {
            return 100; // route priority 
        }
    }
}

Side Note: GroupName and PluginName should be your plugin group name and plugin name.

Hope this helps !

Upvotes: 2

Ananda G
Ananda G

Reputation: 2539

On your plugin which class implements the interface IRouteProvider, you can easily override the route there. Likewise I have a class named RouteProvider in my plugin, So I have Implemented the abstract function RegisterRoutes and simply it can be overrided by

routes.MapRoute("Plugin.Promotion.Combo.SaveGeneralSettings",
                 "Admin/Vendor",
                 new { controller = "CustomAdmin", action = "CustomVendor", },
                 new[] { "Nop.Plugin.Promotion.Combo.Controllers" }
            );

Here Plugin.Promotion.Combo must be replaced by your plugin directory.And using SaveGeneralSettings or any things you want to use that will be your route url

Upvotes: 1

Related Questions