ASP.NET判斷是否為手機登入

蘭博丶專屬發表於2018-05-24
        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;
        }
}

 

相關文章