2014-01-09 65 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

相关问题