使用51度云API进行Global.asax设备检测

时间:2016-04-20 11:29:00

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

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

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

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

以下在不使用51度时有效。 有没有人举例说明如何将51 Degrees Cloud 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
            )
            });

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

由于 托米

1 个答案:

答案 0 :(得分: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属性。