2014-01-09 69 views
3

我在我的網站中使用aspx頁面。當用戶在手機上打開我的Desktop Website時,我想將其重定向到我的Mobile Website。我正在使用C#。如何在手機或平板電腦上重定向到手機頁面?

+0

您的手機鏈接顯示空白頁。 – Dementic

+0

您可以實現重定向,例如通過返回http頭「Location」來響應請求。 – ShinTakezou

+0

您的桌面上的移動網站版本的鏈接返回404錯誤。 – Dementic

回答

3

您可以使用Request.UserAgent在C#中獲得UserAgent

試試這個:

string strUA = Request.UserAgent.Trim().ToLower(); 
bool isMobile = false; 
    if (strUA .Contains("ipod") || strUA .Contains("iphone")) 
     isMobile = true; 

    if (strUA .Contains("android")) 
     isMobile = true; 

    if (strUA .Contains("opera mobi")) 
     isMobile = true; 

    if (strUA .Contains("windows phone os") && strUA .Contains("iemobile")) 
     isMobile = true; 

    if (strUA .Contains("palm") 
     isMobile = true; 

    bool MobileDevice = Request.Browser.IsMobileDevice; 
    if(isMobile == true && MobileDevice == true) 
    { 
     string Url = ""; // Put your mobile site url 
     Response.Redirect(Url); 
    } 

注:IsMobileDevice沒有積極配合新的瀏覽器更新。

一些受歡迎的移動設備/瀏覽器將不會使用這種方式檢測到,因爲Opera Mobile或Android設備不支持ASP.NET瀏覽器文件。

由於這個問題的解決方法是:使用51Degrees.Mobi package. 51Degrees.Mobi package

Read this article: Mobile Device Detection

相關問題