Nopcommerce重写控制器

本文关键字:控制器 重写 Nopcommerce | 更新日期: 2023-09-27 18:01:16

我试图在Nopcommerce的产品控制器中重写方法,但失败了。

在我的插件中,我成功地扩展了服务类,但是当涉及到重写控制器时,我遇到了问题,它只是没有击中断点。

所以我试图覆盖虚拟方法PrepareProductDetailsPageModel在Nop.Web.Controllers.Product

    [NonAction]
    protected virtual ProductDetailsModel PrepareProductDetailsPageModel(Product product, 
        ShoppingCartItem updatecartitem = null, bool isAssociatedProduct = false)
    {
    }

我正在创建新的类ProductController.cs:

  public partial class ProductController :      Nop.Web.Controllers.ProductController
     { 
     #region Fields
    private readonly ICategoryService _categoryService;
    private readonly IManufacturerService _manufacturerService;
    private readonly IProductService _productService;
    private readonly IVendorService _vendorService;
    private readonly IProductTemplateService _productTemplateService;
    private readonly IProductAttributeService _productAttributeService;
    private readonly IWorkContext _workContext;
    private readonly IStoreContext _storeContext;
    private readonly ITaxService _taxService;
    private readonly ICurrencyService _currencyService;
    private readonly IPictureService _pictureService;
    private readonly ILocalizationService _localizationService;
    private readonly IPriceCalculationService _priceCalculationService;
    private readonly IPriceFormatter _priceFormatter;
    private readonly IWebHelper _webHelper;
    private readonly ISpecificationAttributeService _specificationAttributeService;
    private readonly IDateTimeHelper _dateTimeHelper;
    private readonly IRecentlyViewedProductsService _recentlyViewedProductsService;
    private readonly ICompareProductsService _compareProductsService;
    private readonly IWorkflowMessageService _workflowMessageService;
    private readonly IProductTagService _productTagService;
    private readonly IOrderReportService _orderReportService;
    private readonly IBackInStockSubscriptionService _backInStockSubscriptionService;
    private readonly IAclService _aclService;
    private readonly IStoreMappingService _storeMappingService;
    private readonly IPermissionService _permissionService;
    private readonly ICustomerActivityService _customerActivityService;
    private readonly IProductAttributeParser _productAttributeParser;
    private readonly IShippingService _shippingService;
    private readonly MediaSettings _mediaSettings;
    private readonly CatalogSettings _catalogSettings;
    private readonly VendorSettings _vendorSettings;
    private readonly ShoppingCartSettings _shoppingCartSettings;
    private readonly LocalizationSettings _localizationSettings;
    private readonly CustomerSettings _customerSettings;
    private readonly CaptchaSettings _captchaSettings;
    private readonly SeoSettings _seoSettings;
    private readonly ICacheManager _cacheManager;
    #endregion
    #region Constructors
    public ProductController(ICategoryService categoryService,
        IManufacturerService manufacturerService,
        IProductService productService,
        IVendorService vendorService,
        IProductTemplateService productTemplateService,
        IProductAttributeService productAttributeService,
        IWorkContext workContext,
        IStoreContext storeContext,
        ITaxService taxService,
        ICurrencyService currencyService,
        IPictureService pictureService,
        ILocalizationService localizationService,
        IPriceCalculationService priceCalculationService,
        IPriceFormatter priceFormatter,
        IWebHelper webHelper,
        ISpecificationAttributeService specificationAttributeService,
        IDateTimeHelper dateTimeHelper,
        IRecentlyViewedProductsService recentlyViewedProductsService,
        ICompareProductsService compareProductsService,
        IWorkflowMessageService workflowMessageService,
        IProductTagService productTagService,
        IOrderReportService orderReportService,
        IBackInStockSubscriptionService backInStockSubscriptionService,
        IAclService aclService,
        IStoreMappingService storeMappingService,
        IPermissionService permissionService,
        ICustomerActivityService customerActivityService,
        IProductAttributeParser productAttributeParser,
        IShippingService shippingService,
        MediaSettings mediaSettings,
        CatalogSettings catalogSettings,
        VendorSettings vendorSettings,
        ShoppingCartSettings shoppingCartSettings,
        LocalizationSettings localizationSettings,
        CustomerSettings customerSettings,
        CaptchaSettings captchaSettings,
        SeoSettings seoSettings,
        ICacheManager cacheManager)
        : base(categoryService, manufacturerService, productService, vendorService, productTemplateService, productAttributeService,
            workContext, storeContext, taxService, currencyService, pictureService, localizationService, priceCalculationService, priceFormatter, webHelper, specificationAttributeService, dateTimeHelper,
            recentlyViewedProductsService, compareProductsService, workflowMessageService, productTagService, orderReportService, backInStockSubscriptionService, aclService, storeMappingService, permissionService,
            customerActivityService, productAttributeParser, shippingService, mediaSettings, catalogSettings, vendorSettings, shoppingCartSettings, localizationSettings, customerSettings, captchaSettings, seoSettings,
            cacheManager)
    {
        this._categoryService = categoryService;
        this._manufacturerService = manufacturerService;
        this._productService = productService;
        this._vendorService = vendorService;
        this._productTemplateService = productTemplateService;
        this._productAttributeService = productAttributeService;
        this._workContext = workContext;
        this._storeContext = storeContext;
        this._taxService = taxService;
        this._currencyService = currencyService;
        this._pictureService = pictureService;
        this._localizationService = localizationService;
        this._priceCalculationService = priceCalculationService;
        this._priceFormatter = priceFormatter;
        this._webHelper = webHelper;
        this._specificationAttributeService = specificationAttributeService;
        this._dateTimeHelper = dateTimeHelper;
        this._recentlyViewedProductsService = recentlyViewedProductsService;
        this._compareProductsService = compareProductsService;
        this._workflowMessageService = workflowMessageService;
        this._productTagService = productTagService;
        this._orderReportService = orderReportService;
        this._backInStockSubscriptionService = backInStockSubscriptionService;
        this._aclService = aclService;
        this._storeMappingService = storeMappingService;
        this._permissionService = permissionService;
        this._customerActivityService = customerActivityService;
        this._productAttributeParser = productAttributeParser;
        this._shippingService = shippingService;
        this._mediaSettings = mediaSettings;
        this._catalogSettings = catalogSettings;
        this._vendorSettings = vendorSettings;
        this._shoppingCartSettings = shoppingCartSettings;
        this._localizationSettings = localizationSettings;
        this._customerSettings = customerSettings;
        this._captchaSettings = captchaSettings;
        this._seoSettings = seoSettings;
        this._cacheManager = cacheManager;
    }
        protected override ProductDetailsModel PrepareProductDetailsPageModel(Product product, ShoppingCartItem updatecartitem = null, bool isAssociatedProduct = false)
        {
           return base.PrepareProductDetailsPageModel(product, 
           updatecartitem, isAssociatedProduct);
        }
     }

但是,这不会击中我的重写方法。

有谁能帮助我,请让我知道我做错了什么。

当重写TaxService.cs时,我在DependencyRegistrar中注册我的TaxService。

我应该为这个控制器做些什么吗?

谢谢你的帮助

Nopcommerce重写控制器

对于任何遇到这个问题的人,我现在已经解决了。

你需要在DependencyRegistrar中注册你的控制器,基本上是添加这一行…

   public virtual void Register(ContainerBuilder builder, ITypeFinder typeFinder)
   {
      builder.RegisterType<YourPlugin.Controllers.
      ProductController>().As<Nop.Web.Controllers.ProductController>();
   }
   public int Order
   {
        get { return 100; }
   }

这将击中你的重写的方法。

还记得将Order设置为100。不知道下限是多少

使用autoface注册组件是一个很好的解决方案。你也可以在你的routes .cs中做一些改变,让你的新控制器的url为:/plugin/controller/XXX,指向之前的控制器