【问题标题】:Global.asax device detection with 51 degrees cloud APIGlobal.asax 设备检测与 51 度云 API
【发布时间】:2016-04-20 11:28:59
【问题描述】:

有人可以帮忙吗? 我想使用 51Degrees 的免费服务,而不是 Lite 版本,而是 Cloud API (https://51degrees.com/compare-data-options)。

我正在尝试将我的 Global.asax 设置为“平板电脑”和“手机”的显示模式,以便我可以使用:

  • index.cshtml
  • index.tablet.cshtml
  • index.mobile.cshtml

以下在不使用 51 度时有效。 有没有人举个例子,如何将 51 度云 API 与 global.asax 集成以过滤平板电脑/移动设备。

https://51degrees.com/Support/Documentation/APIs/Cloud-API/NET-Cloud

DisplayModeProvider.Instance.Modes.Insert(0, new DefaultDisplayMode("Tablet")
            {
            ContextCondition = (ctx =>
            ctx.Request.UserAgent.IndexOf("iPad", StringComparison.OrdinalIgnoreCase) >= 0 ||
            ctx.Request.UserAgent.IndexOf("Android", StringComparison.OrdinalIgnoreCase) >= 0  &&
            ctx.Request.UserAgent.IndexOf("Mobile", StringComparison.OrdinalIgnoreCase) <= 0
            )
            });

谢谢 汤米

【问题讨论】:

    标签: asp.net-mvc asp.net-mvc-4 global-asax device-detection 51degrees


    【解决方案1】:

    您可以使用您链接的页面上的第一个 C# 示例获取 DeviceType 的值,它可以是 Desktop、SmartPhone 或 Tablet(以及其他一些东西)。比如:

    string json = webClient.DownloadString(String.Format(
      "https://cloud.51degrees.com/api/v1/{0}/match?user-agent={1}&values=DeviceType",
      yourLicenceKey, ctx.Request.UserAgent));
    
    dynamic match = Newtonsoft.Json.Linq.JObject.Parse(json);
    

    那么您对平板电脑的条件是:

    DisplayModeProvider.Instance.Modes.Insert(0, new DefaultDisplayMode("Tablet")
                {
                ContextCondition = (ctx =>
                    match.Values.DeviceType.IndexOf("Tablet", StringComparison) != -1))
                });
    

    您可以通过 URL 查询 DeviceType 的可能值

    https://cloud.51degrees.com/api/v1/[you licence key]/values?propertyname=DeviceType
    

    或者,使用返回 true 或 false 的 IsMobile、IsSmartPhone、IsTablet 和 IsDesktop 属性。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-01-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多