4

First keep in mind I am new to nop/mvc, and despite my best effort can find no solution to this seemingly simple task.

I have been defining custom routes for a plugin I am making, and so far it's been going fine. Any routes I define have worked without issue (Example, I have set routes for "/Dealerlocator" and "Dealer/List")

The issue comes from the fact that there is an already an area defined for '/Admin", so when I try to set a custom route for something like "Admin/Dealer", from what I can tell my route is being resolved by the area and not my custom route. It looks like my controller is never reached, as it's in a different namespace then the one the area route uses, and I get a "The resource cannot be found." error.

So what I'd like to happen is when I go to "Admin/Dealer", it ignores the route set in the area in this one cause, and uses the route I define in the RouteProvider class.

It was suggested using DataTokens would fix this problem. However I cannot get them to work.

My Plugin routing code:

   public partial class RouteProvider : IRouteProvider
{
    public void RegisterRoutes(RouteCollection routes)
    {
        var route = routes.MapRoute(
             "Nop.Plugin.Misc.DealersAdmin",
             "Admin/Dealer",
             new { controller = "DealerAdmin", action = "Index" },
             new[] { "Nop.Plugin.Misc.Dealers.Controllers" }
        );

        route.DataTokens.Add("Area", "Admin");
    }
} 

Nopcommerce's admin area routing:

public class AdminAreaRegistration : AreaRegistration
{
    public override string AreaName
    {
        get
        {
            return "Admin";
        }
    }

    public override void RegisterArea(AreaRegistrationContext context)
    {
        context.MapRoute(
            "Admin_default",
            "Admin/{controller}/{action}/{id}",
            new { controller = "Home", action = "Index", area = "Admin", id = "" },
            new[] { "Nop.Admin.Controllers" }
        );
    }
}

I tried setting a condition on the area registration to not match if the controller is named "Dealer", and that seems to work. But I cannot change the AdminAreaRegistration class as it's part of the core nop framework. I would like to see all the work done in the RouteProvider class. Maybe there is a way to set the priority on my route higher so it is the first one resolved? Thanks.

Haacked
  • 58,045
  • 14
  • 90
  • 114
TomZomW
  • 531
  • 1
  • 5
  • 15
  • I think I found a way to accomplish what I wanted, by changing the route I used from "Admin/Dealer" to something that has at least 5 parts in it (Like "Admin/Plugin/Dealer/Admin/List"), so that the url doesn't get matched by the area route. Does this seem like an acceptable way of doing this? – TomZomW Oct 26 '11 at 18:17

1 Answers1

2

I have also come across this issue a while back, it is to do with route priority. This post helped me alot.

Regarding your comment - There is no reason why you couldn't do so, but alternatively, you might have more luck defining your route as;

context.MapRoute(
  "DealerAdminDefault",
  "Dealer/Admin/{action}/{id}",
  new { controller = "DealerAdmin", action = "Index", id = UrlParameters.Optional }
);

Hope this helps,

Matt

Community
  • 1
  • 1
Matt Griffiths
  • 1,142
  • 8
  • 26
  • Thanks for the post about route priority. It was just tricky for me not being able to change any core nop files. Regarding your suggested route, I know of someone else that tried a similar approach (not putting admin first) , but ended up using the 5 part method anyway as not having admin at the start caused other complications. Even on the nop forums, using a 5 part url starting with Admin seems to be the current accepted solution. – TomZomW Nov 15 '11 at 23:24
  • @Tomas how did you solve it? I have the exact same issue, I want to use an url that is already taken by nopcommerce for my plugin, but I do not want to change the core – JSBach Feb 20 '13 at 01:27
  • The issue noted can be solved in two ways. One is using a 5 part url (like 'Admin/Plugin/Dealer/Admin/List') so that it doesn't conflict with the nopcommerce admin routes. Or as Matt noted, changing the url so it doesn't start with Admin (like 'Dealer/Admin/{action}/{id}'). I personally went with the 5 part url so I could keep the Admin at the start. – TomZomW Mar 26 '13 at 18:43