Can I Navigate Directly to a Plugin View?

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.
11 years ago
I want to create a plugin and be able to navigate directly to the plugin view. Is this possible, or is there a way to place a plugin on a single page?  I've worked through the examples to create a widget plugin, but my new widget plugin appears on every page.

Thank you.
11 years ago
mholyfield wrote:
I want to create a plugin and be able to navigate directly to the plugin view. Is this possible, or is there a way to place a plugin on a single page?  I've worked through the examples to create a widget plugin, but my new widget plugin appears on every page.

Thank you.


You can definitely do this, it can be done on any type of plugin, not only Widget plugin:

1. Create a new View and Action (and Model if needed), like how you would do in a normal MVC application.

2. Make the View an Embedded Resource, and when returning the View from your controller, use the following rule:

return View("Nop.Plugin.Widget.YourPlugin.Views.ViewName", model);


Not that since we've marked the View as an embedded resource, we have to specify the fully-qualified name of the View, as if the View is now a class. So the general rule is:
{Plugin_Default_Namespace}.{View_Folder_Name}.{View_Name_Without_CSHTML}


3. Edit (or add) RouteProvider.cs in your plugin to point to your Action, for example:

routes.MapRoute("ROUTE_NAME",
    "ROUTE_URL",
    new { controller = "YOUR_CONTROLLER", action = "YOUR_ACTION" },
    new[] { "YOUR PLUGIN DEFAULT NAMESPACE" }
);


For this, open the RouteProvder.cs in any other plugins and you'll have a better clue.

4. If everything wires up correctly, you'll be able to visit your link through ROUTE_URL, or by specifying in code:
Url.RouteUrl("ROUTE_NAME")


:)
11 years ago
Perfect.  Thank you Lam Woon.
11 years ago
mholyfield wrote:
Perfect.  Thank you Lam Woon.


Haha. No problem. You can call me Woon Cherk or Lam. Lam is my Surname, Woon Cherk is my given name. :P
11 years ago
Dear all, my problem is more or less the same. I am trying to implement the custom plugin with data access (for nopcommerce version 2.65) that is described https://www.nopcommerce.com/docs/75/plugin-with-data-access.aspx and whenever I try to debug the solution I get the folowing error whenerver I try to see a product details. For example for http://localhost:2619/p/2/25-virtual-gift-card (the database is fullfilled with demo content) I get the following error "The controller for path '/p/2/25-virtual-gift-card' was not found or does not implement IController."
The route is implemented as described in the article
public void RegisterRoutes(RouteCollection routes)
        {
            routes.MapRoute("Nop.Plugin.Other.ProductViewTracker.Log", "tracking/productviews/{productId}", new { controller = "Tracking", action = "Index" }, new[] { "Nop.Plugin.Other.ProductViewTracker.Controllers" });
        }

and in the files  ProductTemplate.SingleVariant.cshtml and ProductTemplate.VariantsInGrid.cshtml  I have added the following line @Html.Action("Index", "Tracking", new { productId = Model.Id }).
This is the line that crashes during debbuging. When I try manually to navigate to a url like the following I get a different problem.
For example
For URL http://localhost:2619/tracking/productviews/23 I get the error No parameterless constructor defined for this object.

Any suggestions?
Thank you In advance
11 years ago
christophad wrote:
Dear all, my problem is more or less the same. I am trying to implement the custom plugin with data access (for nopcommerce version 2.65) that is described https://www.nopcommerce.com/docs/75/plugin-with-data-access.aspx and whenever I try to debug the solution I get the folowing error whenerver I try to see a product details. For example for http://localhost:2619/p/2/25-virtual-gift-card (the database is fullfilled with demo content) I get the following error "The controller for path '/p/2/25-virtual-gift-card' was not found or does not implement IController."
The route is implemented as described in the article
public void RegisterRoutes(RouteCollection routes)
        {
            routes.MapRoute("Nop.Plugin.Other.ProductViewTracker.Log", "tracking/productviews/{productId}", new { controller = "Tracking", action = "Index" }, new[] { "Nop.Plugin.Other.ProductViewTracker.Controllers" });
        }

and in the files  ProductTemplate.SingleVariant.cshtml and ProductTemplate.VariantsInGrid.cshtml  I have added the following line @Html.Action("Index", "Tracking", new { productId = Model.Id }).
This is the line that crashes during debbuging. When I try manually to navigate to a url like the following I get a different problem.
For example
For URL http://localhost:2619/tracking/productviews/23 I get the error No parameterless constructor defined for this object.

Any suggestions?
Thank you In advance


@Html.Action("Index", "Tracking", new { productId = Model.Id })


This is not the correct way to reference the action in your plugin. You need to also tell @Html.Action which in namespace to look for your Action, since you plugin has a different namespace than your Nop.Web.

Try this:


    @Html.Action("Index", "Tracking", new RouteValueDictionary()
    {
        { "Namespaces", "{PLUGIN_NAMESPACE_HERE}" },
        { "area", null },
        { "productId", Model.Id }
    })


For the "I get the error No parameterless constructor defined for this object." error, most probably you have not registered for one (or many) of the services you defined in your Controller constructor. Try registering all the parameters in DependencyRegistrar.cs of your plugin. :)
11 years ago
Thank you very much. I ll try the suggestions you proposed and I ll tell you the result
11 years ago
I have changed it like this

@Html.Action("Index", "Tracking", new RouteValueDictionary()
    {
        { "Namespaces", "{Nop.Plugin.Other.ProductViewTracker}" },
        { "area", null },
        { "productId", Model.Id }
    })

and I get the same error. :(
11 years ago
christophad wrote:
I have changed it like this

@Html.Action("Index", "Tracking", new RouteValueDictionary()
    {
        { "Namespaces", "{Nop.Plugin.Other.ProductViewTracker}" },
        { "area", null },
        { "productId", Model.Id }
    })

and I get the same error. :(


It should be:


    @Html.Action("Index", "Tracking", new RouteValueDictionary()
    {
        { "Namespaces", "Nop.Plugin.Other.ProductViewTracker.Controllers" },
        { "area", null },
        { "productId", Model.Id }
    })


No braces, and use the namespace of the Controller. :)
11 years ago
Hi Woon Cherk and Christoph,

I was getting the same "...was not found or does not implement IController" error and I resolved that by implementing the iController interface on my controller class for the view that's hosting my plugin.  I changed my controller to implement "BaseNopController, IController", and now I'm on to my next issue... I'm getting a blank page, but no error... I'm assuming I need to understand the required code for the IController.Execute method.

Thanks for your help so far!

Monte
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.