【问题标题】:'HttpRequest' does not contain a defination for 'Browser' in nopcommerce 4.2'Http Request' 不包含 nopcommerce 4.2 中'Browser' 的定义
【发布时间】:2019-10-28 17:19:39
【问题描述】:

我收到标题错误。这是完整的错误显示

“HttpRequest”不包含“Browser”的定义,并且找不到接受“HttpRequest”类型的第一个参数的可访问扩展方法“Browser”(您是否缺少 using 指令或程序集引用? )

现在,当我编写代码以检查网站是否在计算机移动中运行时,我会看到此错误。

我也尝试为HTTPRequest 提供参考,但在nuget package 中没有找到

这是我的控制器代码,

public ActionResult MobileBrowser()
{
    var browser = Request.Browser;
    System.Web.HttpBrowserCapabilitiesBase myBrowserCaps = browser;
    if (((System.Web.HttpBrowserCapabilitiesBase)myBrowserCaps).IsMobileDevice)
    {
        ViewBag.Message = "mob";
    }
    else
    {
        ViewBag.Message = "web";
    }
    return PartialView();
}

现在,var browser = Request.Browser;System.Web.HttpBrowserCapabilitiesBase myBrowserCaps = browser;if (((System.Web.HttpBrowserCapabilitiesBase)myBrowserCaps).IsMobileDevice) 中的红线

【问题讨论】:

    标签: asp.net-mvc reference httprequest nopcommerce asp.net-core-2.2


    【解决方案1】:

    你可以试试这个——

    1. 无注入:控制器有 Request 对象,它是 HttpRequest 类型,它包含 IHeaderDictionary 类型的 Header,您将从那里获得浏览器信息。
    公共类 UploadFileController : 控制器 { [HttpGet] 公共 IActionResult 照片() { var brows = Request.Headers["User-Agent"].ToString(); 返回视图(); } [HttpPost] 公共 IActionResult 照片(UserViewModel userViewModel) { 如果(模型状态。IsValid) { } 返回视图(用户视图模型); } }
    1. IHttpContextAccessor 在控制器中注入: 您需要添加 Startup 类中指定类型的单例服务,配置方法services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();。现在你已经准备好注入控制器了 -
    公共类 UploadFileController : 控制器 { 私有只读 IHttpContextAccessor httpContextAccessor; 公共 UploadFileController(IHttpContextAccessor httpContext) { this.httpContextAccessor = httpContext; } [HttpGet] 公共 IActionResult 照片() { var browser = httpContextAccessor.HttpContext.Request.Headers["User-Agent"].ToString(); 返回视图(); } [HttpPost] 公共 IActionResult 照片(UserViewModel userViewModel) { 如果(模型状态。IsValid) { } 返回视图(用户视图模型); } }
    1. IHttpContextAccessor Inject in View:如果你想在视图中有浏览器信息,你需要在Startup类中添加配置方法services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();并注入IHttpContextAccessor。
    @inject Microsoft.AspNetCore.Http.IHttpContextAccessor HttpContextAccessor @{ ViewData["Title"] = "上传图片"; var browser = HttpContextAccessor.HttpContext.Request.Headers["User-Agent"].ToString(); }

    【讨论】:

    • 你能告诉我如何在控制器中实现这个代码吗?因为当时我尝试过,所以它向我显示错误,即 Autofac.Core.DependencyResolutionException: 'An error occurred during the activation of a specific registration...'
    • 嗨,karan,我已经编辑了我的答案,它可能会帮助您解决问题。
    【解决方案2】:

    在 nopCommerce 中,他们提供在移动设备中使用的网站或不使用以下服务

     /// <summary>
        /// Get a value indicating whether the request is made by mobile device
        /// </summary>
        /// <returns></returns>
        public virtual bool IsMobileDevice()
        {
            if (_httpContextAccessor?.HttpContext == null)
                return false;
    
            //we put required logic in try-catch block
            //more info: https://www.nopcommerce.com/boards/t/17711/unhandled-exception-request-is-not-available-in-this-context.aspx
            try
            {
                //we don't parse browscap library here
                //in 99% of cases it's enough to use the approach suggested by http://detectmobilebrowsers.com/
    
                var userAgent = _httpContextAccessor.HttpContext.Request.Headers[HeaderNames.UserAgent].ToString();
                var mobile = _firstMobileDeviceRegex.IsMatch(userAgent) || _secondMobileDeviceRegex.IsMatch(userAgent.Substring(0, 4));
                return mobile;
            }
            catch
            {
                // ignored
            }
    
            return false;
        }
    

    或者代替请求浏览器,你可以使用这个

    var userAgent = _httpContextAccessor.HttpContext.Request.Headers[HeaderNames.UserAgent].ToString();
    

    所以这肯定会有所帮助

    希望对你有帮助

    谢谢

    【讨论】:

    • 你能告诉我如何在控制器中实现这个代码吗?因为当时我尝试过,所以它向我显示错误,即 Autofac.Core.DependencyResolutionException: '在激活特定注册期间发生错误。 这是我的控制器代码:var mobileDevice = _userAgentHelper .IsMobileDevice();
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-02
    • 2018-01-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多