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
mholyfield wrote:
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


Hi Monte,

You actually don't need to specifically inherit IController. BaseNopController already inherits IController, where the full hierarchy is:

BaseNopController -> Controller -> ControllerBase -> IController
11 years ago
Ahh yes.  I had not used the correct controller name.  This works for me now.  Thank you very much!
11 years ago
mholyfield wrote:
Ahh yes.  I had not used the correct controller name.  This works for me now.  Thank you very much!


No problem. :)
11 years ago
Thank you for your posts.
I changed the following line in TrackingController class from

    public class TrackingController : Controller

to

    public class TrackingController : BaseNopController

Now it finds the controller :)

although it still does not work cause I get a different error

Attempt by method 'Nop.Plugin.Other.ProductViewTracker.Controllers.TrackingController.Index(Int32)' to access method 'System.Web.Mvc.Controller.Content(System.String)' failed.

:(
11 years ago
christophad wrote:
Thank you for your posts.
I changed the following line in TrackingController class from

    public class TrackingController : Controller

to

    public class TrackingController : BaseNopController

Now it finds the controller :)

although it still does not work cause I get a different error

Attempt by method 'Nop.Plugin.Other.ProductViewTracker.Controllers.TrackingController.Index(Int32)' to access method 'System.Web.Mvc.Controller.Content(System.String)' failed.

:(


There shouldn't be any different whether you inherit from Controller or BaseNopController.

Mind to share the code in your Controller that cause the error? :)
11 years ago
Here it is
using System.Web.Mvc;
using Nop.Core;
using Nop.Core.Domain.Catalog;
using Nop.Plugin.Other.ProductViewTracker.Domain;
using Nop.Plugin.Other.ProductViewTracker.Services;
using Nop.Services.Catalog;
using Nop.Services.Customers;
using Nop.Core.Plugins;
using Nop.Web.Controllers;

namespace Nop.Plugin.Other.ProductViewTracker.Controllers
{
    public class TrackingController : BaseNopController
    {
        private readonly IProductService _productService;
        private readonly IViewTrackingService _viewTrackingService;
        private readonly IWorkContext _workContext;

        public TrackingController(IWorkContext workContext,
            IViewTrackingService viewTrackingService,
            IProductService productService,
            IPluginFinder pluginFinder)
        {
            _workContext = workContext;
            _viewTrackingService = viewTrackingService;
            _productService = productService;
        }

        [ChildActionOnly]
        public ActionResult Index(int productId)
        {
            //Read from the product service
            Product productById = _productService.GetProductById(productId);

            //If the product exists we will log it
            if (productById != null)
            {
                //Setup the product to save
                var record = new TrackingRecord();
                record.ProductId = productId;
                record.ProductName = productById.Name;
                record.CustomerId = _workContext.CurrentCustomer.Id;
                record.IpAddress = _workContext.CurrentCustomer.LastIpAddress;
                record.IsRegistered = _workContext.CurrentCustomer.IsRegistered();

                //Map the values we're interested in to our new entity
                _viewTrackingService.Log(record);
            }

            //Return the view, it doesn't need a model
           // return Content("");
            return View();
        }
    }
}



The code for the whole plugin can be found here https://www.nopcommerce.com/docs/75/plugin-with-data-access.aspx
11 years ago
christophad wrote:
Here it is
using System.Web.Mvc;
using Nop.Core;
using Nop.Core.Domain.Catalog;
using Nop.Plugin.Other.ProductViewTracker.Domain;
using Nop.Plugin.Other.ProductViewTracker.Services;
using Nop.Services.Catalog;
using Nop.Services.Customers;
using Nop.Core.Plugins;
using Nop.Web.Controllers;

namespace Nop.Plugin.Other.ProductViewTracker.Controllers
{
    public class TrackingController : BaseNopController
    {
        private readonly IProductService _productService;
        private readonly IViewTrackingService _viewTrackingService;
        private readonly IWorkContext _workContext;

        public TrackingController(IWorkContext workContext,
            IViewTrackingService viewTrackingService,
            IProductService productService,
            IPluginFinder pluginFinder)
        {
            _workContext = workContext;
            _viewTrackingService = viewTrackingService;
            _productService = productService;
        }

        [ChildActionOnly]
        public ActionResult Index(int productId)
        {
            //Read from the product service
            Product productById = _productService.GetProductById(productId);

            //If the product exists we will log it
            if (productById != null)
            {
                //Setup the product to save
                var record = new TrackingRecord();
                record.ProductId = productId;
                record.ProductName = productById.Name;
                record.CustomerId = _workContext.CurrentCustomer.Id;
                record.IpAddress = _workContext.CurrentCustomer.LastIpAddress;
                record.IsRegistered = _workContext.CurrentCustomer.IsRegistered();

                //Map the values we're interested in to our new entity
                _viewTrackingService.Log(record);
            }

            //Return the view, it doesn't need a model
           // return Content("");
            return View();
        }
    }
}



The code for the whole plugin can be found here https://www.nopcommerce.com/docs/75/plugin-with-data-access.aspx


I don't see any problem with your code (or rather the sample code) except that the call to

return View();


might fail in a plugin. What errors are you getting currently, and how are you accessing the action (as in what are the code you using in @Html.Action)? :)
11 years ago
I did what exactly the tutorial says. Inside ProductTemplate.SingleVariant.cshtml and ProductTemplate.VariantsInGrid.cshtml  I added the following line
@Html.Action("Index", "Tracking", new { productId = Model.Id })

Inside the trackingcontroller I used both return Content(""); and return View(); but both failed
11 years ago
Apart from that. I have one more question. The tutorial suggests creating a plugin using Dependency Injection. To be honest I have not really understood how dependency injection works. Later on I saw how the others plugins are created and they do not use dependency injection. And my question is. Whenever I create a new widget is it obligatory to use Dependency Injection or I can create it just using Model, View Controller Pattern as a normal MVC application?

Thank you very much in advance.
You are very helpfull
11 years ago
Any plugin using Services will use Dependency Injection.  In a nutshell, all you need to do is create a local variable to reference the service and assign that variable in the class constructor.  E.g.

    public partial class PurchasedAllProductsDiscountRequirementRule : BasePlugin, IDiscountRequirementRule
    {
        private readonly IOrderService _orderService;

        public PurchasedAllProductsDiscountRequirementRule(IOrderService orderService)
        {
            this._orderService = orderService;
        }

(and add appropriate "using"s to reference the services)
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.