Admin plugin route

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.
12 years ago
Hi Guys,

I have a question about MVC/ NopCommerce 2.2.

I'm developing a plugin that will need some admin back-end functionality and I can't get the request to pick up the controller.

I enabled the Route Debugger and it actually says that the route is determined.

URL: /admin/Plugins/PriceMarge/Admin/List

Matched Route: admin/Plugins/PriceMarge/Admin/{action}/{id}

But when I set some breakpoints the Controller is never called.

The routing is:


var route2 = routes.MapRoute("pricemargeadmin2",
        "admin/Plugins/PriceMarge/Admin/{action}/{id}",
        new { controller = "PriceMargeAdminController", action = "List", id = "" },
        new[] { "TP.Nop.Plugin.Catalog.PriceMarge.Controllers" }
    );

route2.DataTokens.Add("area", "admin");



When doing the same for not-admin functionality it works OK.
I have copied the view (List) to the /Views/Controllername/ and the /Administration/Views/Controllername/

Does someone have a suggestion for me ??

Kind regards
12 years ago
Hi stephen,

If your route is found but still your controller is not found then please read this post

Hope this helps!
12 years ago
Hi 7Spikes,

Thanks for your reply!

I did a rebuild / clean etc on the solution & finally emptied the whole asp.net temporary internet files. First had to close all VS2010 Solutions and stop IIS for that.

The error remains :(

The resource cannot be found.
Description: HTTP 404. The resource you are looking for (or one of its dependencies) could have been removed, had its name changed, or is temporarily unavailable.  Please review the following URL and make sure that it is spelled correctly.


I enabled the RouteDebugger and it still says:

Matched Route: admin/Plugins/PriceMarge/Admin/{action}/{id}



The controller has 2 views:


        [HttpGet]
        public ActionResult Index()
        {
            return RedirectToAction("List");
        }

        [HttpGet]
        public string List()
        {
            //if (!_permissionService.Authorize(StandardPermissionProvider.ManageCatalog))
            //    return AccessDeniedView();
            //return View("TP.Nop.Plugin.Catalog.PriceMarge.Views.PriceMargeAdmin.List");
            //return View();

            return "HOI";
        }



I tried to return the default view, or just a plain text string as you can see above.

Kind regards
12 years ago
I am trying to create an admin plugin and ran into what looks to be the same or similar problem.

It looks like this issue is covered in the post...

https://www.nopcommerce.com/boards/t/12009/plugin-and-routeprovider.aspx

...but now we have at least 3 people that have tried this solution and it no longer seems to work. Are we missing something or did something change?

From what I can tell the default admin maproute from AdminAreaRegistration.cs  is still being used instead of my plugin one, even after adding the Datatoken as noted by 7Spikes. Just to test that, changing my admin plugin controller's namespace to "Nop.Admin.Controllers" does at least allow my controller to be found...

Thanks.
12 years ago
I really hope some of the nopCommerce hero's can look into this. Till then I won't spent any more time on this.. My MVC experience is not yet good enough..
12 years ago
stephen.maij wrote:
I really hope some of the nopCommerce hero's can look into this. Till then I won't spent any more time on this.. My MVC experience is not yet good enough..


Try this:


routes.MapRoute("pricemargeadmin2", "admin/Plugins/PriceMarge/Admin/{action}/{id}",
        new { controller = "PriceMargeAdmin", action = "List", id = UrlParameter.Optional },
        new[] { "TP.Nop.Plugin.Catalog.PriceMarge.Controllers" }
/*  
Make sure this namespace is correct. TP.Nop.Plugin... does not follow convention. Some code might depend on the plugin being prefixed with Nop.Plugin
*/

    );

12 years ago
Thanks for the response Sklyer. This seemingly simple tasks really does seem to be a head scratcher.

My route follows what you recommended but I'm having the same issue.

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");


I have also tried other combinations of setting the area and using DataTokens as the code below shows, but nothing seems to work.

routes.MapRoute(
   "Nop.Plugin.Misc.DealersAdmin",
   "Admin/Dealer",
   new { controller = "DealerAdmin", action = "Index", area = "Admin" },
   new[] { "Nop.Plugin.Misc.Dealers.Controllers" }
).DataTokens.Add("Area", "Admin");


I tried setting a negative condition on the area route to not match if the controller is named "Dealer", and that seems to work. Going to "Admin/Dealer" it was able to find my plugin controller in its proper namespace. But that envolves changing the AdminAreaRegistration.cs file which I don't want to do.
12 years ago
Geralt wrote:
Thanks for the response Sklyer. This seemingly simple tasks really does seem to be a head scratcher.

My route follows what you recommended but I'm having the same issue.

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");


I have also tried other combinations of setting the area and using DataTokens as the code below shows, but nothing seems to work.

routes.MapRoute(
   "Nop.Plugin.Misc.DealersAdmin",
   "Admin/Dealer",
   new { controller = "DealerAdmin", action = "Index", area = "Admin" },
   new[] { "Nop.Plugin.Misc.Dealers.Controllers" }
).DataTokens.Add("Area", "Admin");


I tried setting a negative condition on the area route to not match if the controller is named "Dealer", and that seems to work. Going to "Admin/Dealer" it was able to find my plugin controller in its proper namespace. But that envolves changing the AdminAreaRegistration.cs file which I don't want to do.


Hi Geralt,

My advice was actually for the topic starter.
I think it is possible your route is too vague and matches other patterns. I'll look into this further to see if I can replicate the issue, but if you reverse your path it will probably work. Here are some routes for a plugin I'm currently working on. They are likely going to change, but they definitely work as they are written.


            routes.MapRoute("Nop.Plugin.Payments.Adyen.Public", "adyen/public/{action}", new {controller = "Public", action = "Index"}, new[] { "Nop.Plugin.Payments.AdyenHosted.Controllers" });
            routes.MapRoute("Nop.Plugin.Payments.Adyen.Settings", "adyen/settings/{action}", new {controller = "Settings", action = "Index"}, new[] { "Nop.Plugin.Payments.AdyenHosted.Controllers" });
            routes.MapRoute("Nop.Plugin.Payments.Adyen.Account", "adyen/admin/account/{action}/{id}", new {controller = "Account", action = "Index", id = UrlParameter.Optional }, new[] { "Nop.Plugin.Payments.AdyenHosted.Controllers" });
12 years ago
Sorry for jumping on your reply Skyler. I wasn't trying to hijack a thread or anything, I just thought the problems me and the original poster were having are the same, and was hoping a fix for one of us would benefit us all.

I know my "Admin/Dealer" pattern is also matching the general area "Admin" pattern set in "AdminAreaRegistration.cs". Is the accepted solution then to make my admin url more specific so that it doesn't match the default area pattern? Something like "Admin/Plugin/MyCompany/DealerAdmin/List", that would trigger my route and not the catchall admin one. Could it be that simple? :)

Either way you've given me some new ideas to play around with.
12 years ago
Geralt wrote:
Sorry for jumping on your reply Skyler. I wasn't trying to hijack a thread or anything, I just thought the problems me and the original poster were having are the same, and was hoping a fix for one of us would benefit us all.

I know my "Admin/Dealer" pattern is also matching the general area "Admin" pattern set in "AdminAreaRegistration.cs". Is the accepted solution then to make my admin url more specific so that it doesn't match the default area pattern? Something like "Admin/Plugin/MyCompany/DealerAdmin/List", that would trigger my route and not the catchall admin one. Could it be that simple? :)

Either way you've given me some new ideas to play around with.


Yes, your path should only match the route you've defined. If your path matches another route (and that route is found first) then it will look for your controller inside of the first routes provided namespaces. Obviously your controllers wouldn't be in that namespace and you would see a 404 when trying to access your new controllers. You can still use patterns in your routes, but they should be more specific (e.g. "[plugin-name]/admin/{controller}/{action}").

p.s. You didn't hijack the post, but the solution I presented was almost the same as what you posted above. The topic starter has a controller of "PriceMargeAdminController" and they should not have included "Controller" otherwise the engine will search for a class named "PriceMargeAdminControllerController".
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.