首页 > 解决方案 > 如何在 nopcommerce 4.2 的通用控制器中使用 IsMobileDevice()

问题描述

我想在通用控制器中使用 IsMobileDevice()。但是当我使用这种方法时,它向我显示错误,即

1. 激活特定注册时发生错误。有关详细信息,请参阅内部异常。注册:Activator = CommonController (ReflectionActivator), Services = [Nop.Web.Controllers.CommonController], Lifetime = Autofac.Core.Lifetime.CurrentScopeLifetime, Sharing = None, Ownership = OwnedByLifetimeScope ---> 没有找到带有 'Autofac 的构造函数'Nop.Web.Controllers.CommonController' 类型上的 .Core.Activators.Reflection.DefaultConstructorFinder' 可以使用可用的服务和参数调用:

2. 无法解析构造函数'Void.ctor(Nop.Core.Domain.Security.CaptchaSettings, Nop.Core.Domain.Common.CommonSettings, Nop.Web.Factories.ICommonModelFactory, Nop.Services.Directory.ICurrencyService、Nop.Services.Logging.ICustomerActivityService、Nop.Services.Common.IGenericAttributeService、Nop.Services.Localization.ILanguageService、Nop.Services.Localization.ILocalizationService、Nop.Services.Logging.ILogger、Nop。 Core.IStoreContext、Nop.Web.Framework.Themes.IThemeContext、Nop.Services.Vendors.IVendorService、Nop.Core.IWorkContext、Nop.Services.Messages.IWorkflowMessageService、Nop.Core.Domain.Localization.LocalizationSettings、Nop.Core。 Domain.Common.SitemapSettings、Nop.Core.Domain.Common.SitemapXmlSettings、Nop.Core.Domain.StoreInformationSettings、Nop.Core。Domain.Vendors.VendorSettings,Nop.Services.Helpers.UserAgentHelper)'。

这是我如何在通用控制器中使用此方法的代码行

var mobileDevice = _userAgentHelper.IsMobileDevice();

这里是领域

private readonly IHttpContextAccessor _httpContextAccessor;

为什么在运行时显示错误?

标签: asp.net-mvcasp.net-core-2.1nopcommerce

解决方案


我刚刚在 nopCommerce 4.20 中检查了通用控制器中的 IsMobileDevice 方法及其工作正常,它将返回 true 或 false,我尝试过使用 google chrome

这是我实现的代码

private readonly IUserAgentHelper _userAgentHelper;
public CommonController(IUserAgentHelper userAgentHelper)
{
  _userAgentHelper = userAgentHelper;
}


public virtual IActionResult ContactUs()
    {
        var model = new ContactUsModel();

        var mobileDevice = _UserAgentHelper.IsMobileDevice();
        if(mobileDevice)
           return true;

        model = _commonModelFactory.PrepareContactUsModel(model, false);
        return View(model);
    }

在此处输入图像描述


推荐阅读