读书人

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

发布时间: 2012-09-17 12:06:51 作者: rapoo

怎么判断 手机用户和电脑用户登录网站呢

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"/> 

读书人网 >asp.net

热点推荐