3

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();
    }
}
Divyang Desai
  • 7,483
  • 13
  • 50
  • 76
Optimus
  • 2,200
  • 8
  • 37
  • 71
  • Do you want to override route in a plugin? – Divyang Desai Jan 10 '17 at 12:32
  • @Div yep I need to override in my plugin – Optimus Jan 10 '17 at 12:33
  • Okay, can you show us `RouteProvider` file of your plugin! – Divyang Desai Jan 10 '17 at 12:38
  • @Div I'm new to nop is there any specif route provider file for nop admin – Optimus Jan 10 '17 at 12:57
  • No worries, yes *(but not specific file for admin)* , plugin should have `RouteProvider`(which contains your plugin all routes) file. Please refer [How to write a nopCommerce plugin](http://docs.nopcommerce.com/display/en/How+to+write+a+nopCommerce+plugin). In RouteProvider.cs file you can override a route. – Divyang Desai Jan 10 '17 at 13:03
  • I could not find `AdminController` and `Vendor` in nop defaults, where it's located – Divyang Desai Jan 10 '17 at 17:35
  • @div The code I provided is sample code. What I want is, When user clicks on the customer search tab I need to re route it to my custom controller – Optimus Jan 12 '17 at 09:35
  • Okay, you've mentioned, for admin route in the question, so I've added for admin route. let me know if you want to search then i will be add for the same :) – Divyang Desai Jan 12 '17 at 10:21

2 Answers2

2

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 !

Divyang Desai
  • 7,483
  • 13
  • 50
  • 76
1

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

Ananda G
  • 2,389
  • 23
  • 39