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