Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net mvc 如何在nopcommerce 4.2的公共控制器中使用IsMobileDevice()_Asp.net Mvc_Asp.net Core 2.1_Nopcommerce - Fatal编程技术网

Asp.net mvc 如何在nopcommerce 4.2的公共控制器中使用IsMobileDevice()

Asp.net mvc 如何在nopcommerce 4.2的公共控制器中使用IsMobileDevice(),asp.net-mvc,asp.net-core-2.1,nopcommerce,Asp.net Mvc,Asp.net Core 2.1,Nopcommerce,我想在公共控制器中使用IsMobileDevice()。但当我使用这种方法时,它会向我显示错误,即 1。激活特定注册时发生错误。有关详细信息,请参见内部异常。注册:Activator=CommonController(ReflectionActivator),Services=[Nop.Web.Controllers.CommonController],life=Autofac.Core.life.currentScopelife,Sharing=None,Ownership=OwnedByLi

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

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

2。无法解析构造函数Void.ctor的参数“Nop.Services.Helpers.UserAgentThelper UserAgentThelper”(Nop.Core.Domain.Security.captchessettings,Nop.Core.Domain.Common.CommonSettings,Nop.Web.Factories.ICommonModelFactory,Nop.Services.Directory.ICurrencyService,Nop.Services.Logging.icCustomerActivityService,Nop.Services.Common.IGenericAttributeService,Nop.Services.本地化.ILanguageService,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.本地化设置、Nop.Core.Domain.Common.SitemapSettings、Nop.Core.Domain.Common.SitemapXmlSettings、Nop.Core.Domain.Domain。StoreInformationSettings,Nop.Core.Domain.Vendors.VendorSettings, 否。服务。助手。用户代理程序)。

下面是我在common controller中如何使用此方法的代码行

var mobileDevice = _userAgentHelper.IsMobileDevice();
这是场地

private readonly IHttpContextAccessor _httpContextAccessor;

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

我刚刚在nopCommerce 4.20中检查了常见控制器中的IsMobileDevice方法及其工作情况, 它将返回真或假, 我试过谷歌浏览器

下面是我已经实现的代码

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);
    }

显示的错误表明,由于缺少依赖项,因此没有创建控制器。因此,甚至没有调用您显示的行;在创建控制器时,错误发生在这之前

特别是,您需要一个
UserAgentHelper
实例,但Autofac不知道如何获取它。服务依赖项应该由其接口请求


简而言之,您应该需要一个
IUserAgentHelper
,而不是
UserAgentHelper

您可能缺少在构造函数中声明的权限,请您简要说明您的代码,并且我已经回复了答案,请检查它