在nopcommerce 4.2中,“ HttpRequest”不包含对“浏览器”的定义

时间:2019-06-14 05:30:45

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

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

'HttpRequest'不包含'Browser'的定义,并且找不到可访问的扩展方法'Browser'接受类型为'HttpRequest'的第一个参数(您是否缺少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)

中得到了红线

2 个答案:

答案 0 :(得分:0)

在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();

所以这肯定会有所帮助

希望这对您有帮助

谢谢

答案 1 :(得分:0)

您可以尝试-

  1. 没有注入:控制器具有HttpRequest类型的Request对象,它包含IHeaderDictionary类型的Header,您将从此处获取浏览器信息。
    public class UploadFileController : Controller
           {
                [HttpGet]
                public IActionResult Photo()
                {
                      var brows = Request.Headers["User-Agent"].ToString();

                      return View();
                }

                [HttpPost]
                public IActionResult Photo(UserViewModel userViewModel)
                {
                        if (ModelState.IsValid)
                        {
                        }
                     return View(userViewModel);
               }
          }
  1. IHttpContextAccessor注入控制器: 您需要添加在启动类的配置方法services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();中指定的类型的单例服务。现在您可以注入控制器了-
     public class UploadFileController : Controller
            {
                private readonly IHttpContextAccessor httpContextAccessor;
                public UploadFileController(IHttpContextAccessor  httpContext)
                {
                    this.httpContextAccessor = httpContext;
                }

                [HttpGet]
                public IActionResult Photo()
                {
                    var browser = httpContextAccessor.HttpContext.Request.Headers["User-Agent"].ToString();

                    return View();
                }

                [HttpPost]
                public IActionResult Photo(UserViewModel userViewModel)
                {
                    if (ModelState.IsValid)
                    {
                    }
                    return View(userViewModel);
                }
            }
  1. IHttpContextAccessor在视图中注入:如果要在视图中包含浏览器信息,则需要在Startup类,配置方法services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();中添加并注入IHttpContextAccessor。
 @inject Microsoft.AspNetCore.Http.IHttpContextAccessor HttpContextAccessor

      @{
          ViewData["Title"] = "Upload Picture";

          var browser = HttpContextAccessor.HttpContext.Request.Headers["User-Agent"].ToString();
      }