4
votes

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.

1
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

1 Answers

2
votes

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