0

任何人都可以帮忙吗? 我想使用51Degrees的免费服务而不是Lite版本,但是使用Cloud APIhttps://51degrees.com/compare-data-options)。Global.asax设备检测与51度云API

我想设置我的Global.asax有对“平板电脑”和“移动”的显示模式,这样我就可以使用:

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

以下工作时不使用51度。 有没有人有过如何整合51度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 
      ) 
      }); 

感谢 汤米

回答

1

你可以得到设备类型的值可以是台式机,智能手机或平板电脑(加上一些其他的东西)通过使用第一个C#示例您链接的页面。喜欢的东西:

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查询设备类型的可能值

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

或替代,使用IsMobile, IsSmartPhone,IsTablet和IsDesktop属性返回true或false。