asp.net使用用户代理字符串检测任何平板电脑设备

时间:2011-09-09 17:22:08

标签: asp.net asp.net-mvc user-agent tablet

是否可以通过服务器端代码检测来自平板电脑设备(iPad,Android等)的请求?

编辑:我需要知道它是Android平板电脑而不是Android手机。

1 个答案:

答案 0 :(得分:4)

来自Detecting mobile device user agents in ASP.NET (Android)

 //for Mobile device 
    protected override void OnInit(EventArgs e) 
    { 

        string userAgent = Request.UserAgent; 
        if (userAgent.Contains("BlackBerry") 
          || (userAgent.Contains("iPhone") || (userAgent.Contains("Android")))) 
        { 
            //add css ref to header from code behind 
            HtmlLink css = new HtmlLink(); 
            css.Href = ResolveClientUrl("~/mobile.css"); 
            css.Attributes["rel"] = "stylesheet";  
            css.Attributes["type"] = "text/css";  
            css.Attributes["media"] = "all";  
            Page.Header.Controls.Add(css); 
        }       
    }