Search code examples
c#routesasp.net-core-mvcnopcommerce

NopCommerce routing: How to correctly create and make use of plugin routes in NC 4.30


My task is to import a feature from an existing application (classic MVC5) into NopCommerce 4.30. The goal is to have a button somewhere on the actual shop's page that redirects to the plugin's Index view providing the actual features to the users.

This is my first time working with NC. Having read the overall NC documentation, I figured the best approach would be to create a plugin containing the desired functionality. Following the instructions for creating a plugin in NC 4.30 (cf. here), I was able to create and install the plugin.

However, I cannot seem to open any (custom) plugin views from within the application. Taking a look at the plugins shipped with the NC base files, I noticed that most of the plugins had a file named RouteProvider.cs that contained routing information. Based on those files, I created my own simple version and added it to the project, e.g. (sample only)

 public class RouteProvider : IRouteProvider
    {
        private const string ViewNameIndex = "Plugin.Misc.MyPlugin.Index";
        private const string RoutePattern = "Plugins/Index";


        /// <summary>
        /// Register routes
        /// </summary>
        /// <param name="endpointRouteBuilder">Route builder</param>
        public void RegisterRoutes(IEndpointRouteBuilder endpointRouteBuilder)
        {
            endpointRouteBuilder.MapControllerRoute(ViewNameIndex, RoutePattern,
                new { controller = "MyController", action = "Index" });
        }

        /// <summary>
        /// Gets a priority of route provider
        /// </summary>
        public int Priority => 0;
    }

However, I still cannot use any plugin URL.

Moreover, I do not quite understand the purpose of the first and second parameter in MapControllerRoute and how they affect the URL being used. I could not infer their usage from the sample I used, cf. below. (I took the liberty of adding ImportContactRoute and UnsubscribeContactRoute from SendinBlueDefault.cs to this sample)


public class RouteProvider : IRouteProvider
    {
        private const string ImportContactsRoute => "Plugin.Misc.SendinBlue.ImportContacts";
        private const string UnsubscribeContactRoute => "Plugin.Misc.SendinBlue.Unsubscribe";

        /// <summary>
        /// Register routes
        /// </summary>
        /// <param name="endpointRouteBuilder">Route builder</param>
        public void RegisterRoutes(IEndpointRouteBuilder endpointRouteBuilder)
        {
            endpointRouteBuilder.MapControllerRoute(ImportContactsRoute, "Plugins/SendinBlue/ImportContacts",
                new { controller = "SendinBlue", action = "ImportContacts" });

            endpointRouteBuilder.MapControllerRoute(UnsubscribeContactRoute, "Plugins/SendinBlue/UnsubscribeWebHook",
                new { controller = "SendinBlue", action = "UnsubscribeWebHook" });
        }

        /// <summary>
        /// Gets a priority of route provider
        /// </summary>
        public int Priority => 0;
    }

What is Plugin.Misc.SendinBlue.ImportContacts trying to match here? There is neither a corresponding folder in Plugins\Nop.Plugin.Misc.SendinBlue\ nor in Presentation\Nop.Web\Plugins\Misc.SendinBlue\.

I assume that Plugins/SendinBlue/ImportContacts refers to the actual route that might be entered as a URL, but again I am not sure about it at this stage.

Could somebody please explain how I might correctly create and access routes to my plugin's views from within the application, or else provide an alternative approach in case I was barking up the wrong tree.

Update While researching ways to resolve the issue, I came across the following post in the NC forum. The post linked to this page, which describes three "Ways to display Views in Your NopCommerce Plugins":

  1. Embedded Resources
  2. Theme Override
  3. Custom View Engine

As valuable as it is, you'll notice that the post dates back to 2013. The question is whether the approaches are still valid, given the fact that the technology has changed.

I've already tried the first approach of making my View's into embedded resources to no avail.

What would be the correct approache(s) for NC 4.30 in this case?


Solution

  • I gave it another shot and I would like to point out that using approach 1.Embedded resources (cf. above) actually works. I haven't tried the other two approaches due to a lack of understanding regarding these topics.

    Some issues remain, but in my opinion, this question has been answered.