ASP.NET判断是否为手机登录

protected void Page_Load(object sender, EventArgs e)
        {
            MobileHandle();
           }

页面加载时候判断是否为手机登录

protected void MobileHandle()
        {
            string mobilePath = PublicFunction.GetConfigValue("MobilePath");//手机页面的路径
            if (!string.IsNullOrEmpty(mobilePath))
            {
                bool isMobile = PublicFunction.IsMobile(Request.UserAgent);
                if (isMobile)
                {
                    Response.Redirect(mobilePath);//为手机登录的话跳转手机页面
                }
            }
        }
public class PublicFunction
    {
        static string[] mobileTag = { "iphone", "ios", "ipad", "android", "mobile" };

        /// <summary>
        /// 判断是否是手机打开
        /// </summary>
        /// <param name="userAgent">用户浏览器代理信息</param>
        /// <returns></returns>
        public static bool IsMobile(string userAgent)
        {
            bool result = false;
            userAgent = userAgent.ToLower();
            foreach (string sTmp in mobileTag)
            {
                if (userAgent.Contains(sTmp))
                {
                    result = true;
                    break;
                }
            }
            return result;
        }
}

相关推荐