当前位置: 代码迷 >> ASP.NET >> 如何判断 手机用户和电脑用户登录网站呢
  详细解决方案

如何判断 手机用户和电脑用户登录网站呢

热度:4269   发布时间:2013-02-25 00:00:00.0
怎么判断 手机用户和电脑用户登录网站呢
C# code
public static bool GetClientWeb()  {  bool result = false;  string clientType = string.Concat(HttpContext.Current.Request.UserAgent);  if (clientType.ToLower().Contains("mozilla") || clientType.ToLower().Contains("opera"))  {  result = true;  }  return result;  }

刚才测试下返回值都是true

有大虾弄过吗

------解决方案--------------------------------------------------------
C# code
  /// <summary>    /// 判断手机用户UserAgent    /// </summary>    /// <returns></returns>    private bool IsMobile()    {               HttpContext context = HttpContext.Current;        if (context != null)        {            HttpRequest request = context.Request;            if (request.Browser.IsMobileDevice)                return true;            string MobileUserAgent=System.Configuration.ConfigurationManager.AppSettings["MobileUserAgent"];            Regex MOBILE_REGEX = new Regex(MobileUserAgent);            if (string.IsNullOrEmpty(request.UserAgent) || MOBILE_REGEX.IsMatch(request.UserAgent.ToLower()))                return true;                                          }        return false;    }以下为web.config配置里边的<add key="MobileUserAgent" value="iphone|android|nokia|symbian|sony|ericsson|mot|samsung|sgh|lg|philips|panasonic|alcatel|lenovo|cldc|midp|wap|mobile|blackberry|windows ce|motorola|mqqbrowser|ucweb"/>
  相关解决方案