【问题标题】:Detect mobile device in ASP.NET Core在 ASP.NET Core 中检测移动设备
【发布时间】:2016-12-23 06:00:40
【问题描述】:

我有一个应用程序,它使用移动视图和桌面视图作为不同的 html 页面。现在我将其移至 Asp.Net 核心。由于某些技术原因,我没有考虑 Bootstrap。我必须在 StartUp 中检测请求是否来自 Mobile 以加载相应的布局页面。我怎样才能做到这一点?寻找类似于 IsMobileDevice 的东西。已经尝试过 MvcDeviceDetector 0.1.0-t00349acaa。它没有工作,因为我使用的是 .net 4.6.1 版。

【问题讨论】:

标签: asp.net asp.net-mvc asp.net-core


【解决方案1】:

我找到了很棒的图书馆。它非常易于使用。我不确定它是否 100% 可靠,但它涵盖了我所有的案例。

例子:

    public class HomeController : Controller
    {           
        private readonly IDevice device;   

        public HomeController(IDeviceResolver deviceResolver)
        {                
            this.device = deviceResolver.Device
        }

        public IActionResult Index()
        {
            if(device.Type == DeviceType.Desktop)
            {
               //some logic
            }
            else if(device.Type == DeviceType.Mobile)
            {
               //some logic
            }
            else if(device.Type == DeviceType.Tablet)
            {
               //some logic
            }
        }
     }

Device detection .NET CORE

谢谢旺卡奈

【讨论】:

  • 我们可以全局检查设备而不是在每个控制器或操作中吗?实际上我想为移动设备渲染移动视图,为桌面设备渲染桌面视图
  • 我认为可以在中间件中以同样的方式完成。
【解决方案2】:

您可以使用此处列出的手动方法: https://stackoverflow.com/a/13086894/1419970

或者你可以使用这个库:http://www.nuget.org/packages/51Degrees.mobi/3.2.10.3-beta

两者都对你有用。

【讨论】:

  • 无法为 '.NETFramework,Version=v4.6.1' 解析 '51Degrees.mobi (>= 3.2.9.1)'。
  • **无法为 '.NETFramework,Version=v4.6.1' 解析 '51Degrees.mobi (>= 3.2.9.1)'。 ** 这是我尝试 51 度时遇到的错误。我猜它不支持 .net 4.6.1 版。
  • 我想它会给我一个解决方案。只是我需要将方法更改为使用 .net core 进行压缩。
【解决方案3】:

或者你可以使用这个免费的图书馆DeviceDetector.NET

这是流行的 PHP device-detector 库到 C# 的移植。

Here是怎么用的。

DeviceDetectorNET.DeviceDetector.SetVersionTruncation(VersionTruncation.VERSION_TRUNCATION_NONE); 
var userAgent = Request.Headers["User-Agent"];
var result = DeviceDetectorNET.DeviceDetector.GetInfoFromUserAgent(userAgent);
var output = result.Success ? result.ToString().Replace(Environment.NewLine, "<br />") : "Unknown";

【讨论】:

    【解决方案4】:

    简单的解决方案

         public static class Extentions
            {
                public static bool IsMobile(string userAgent)
                {
                    if (string.IsNullOrEmpty(userAgent))
                        return false;
        //tablet
                    if (Regex.IsMatch(userAgent, "(tablet|ipad|playbook|silk)|(android(?!.*mobile))", RegexOptions.IgnoreCase))
                        return true;
        //mobile
                    const string mobileRegex =
                        "blackberry|iphone|mobile|windows ce|opera mini|htc|sony|palm|symbianos|ipad|ipod|blackberry|bada|kindle|symbian|sonyericsson|android|samsung|nokia|wap|motor";
        
                    if (Regex.IsMatch(userAgent, mobileRegex, RegexOptions.IgnoreCase)) return true;
       //not mobile 
                    return false;
                }
            }
    

    使用:

    var isMobile = Extentions.IsMobile(Context.Request.Headers["user-agent"].ToString());
    

    【讨论】:

    • 我找到的最佳解决方案。无需安装任何软件包并解决类关系和正确使用问题。谢谢!
    【解决方案5】:

    这是我用于使用中间件的 .NET6 项目的解决方案。 https://github.com/totpero/DeviceDetector.NET

    using DeviceDetectorNET;
           
    public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
    {
        app.Use(async (context, next) =>
        {
             var detector = new DeviceDetector(context.Request.Headers["User-Agent"].ToString());
             detector.SetCache(new DictionaryCache());
             detector.Parse();
    
            if (detector.IsMobile())
            {
                context.Items.Remove("isMobile");
                context.Items.Add("isMobile", true);
            }
            else
            {
                context.Items.Remove("isMobile");
                context.Items.Add("isMobile", false);
            }
    
            context.Items.Remove("DeviceName");
            context.Items.Add("DeviceName", detector.GetDeviceName());
    
            await next();
    
        });
    }
    

    用法

      var deviceName = HttpContext.Items["DeviceName"].ToString();
      var isMobile = Convert.ToBoolean(HttpContext.Items["isMobile"]);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2010-09-06
      • 2010-11-20
      • 2021-04-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多