4

我想使用我的 .cs 代码隐藏,Page_PreInit 或 Page_Load 来检测移动浏览器并重定向。我遇到了这个:

protected void Page_PreInit(object sender, EventArgs e) 
{ 
    if (Request.Browser.IsMobileDevice) 
    { 
        { 
          Response.Redirect("~/default_mobile.aspx"); 
        }

    } 
} 

它似乎不起作用。有人可以建议更正吗?另外,您是否知道不重定向的示例,而只是将 .aspx 页面上的元素替换为另一个元素(即;将 Silverlight 电影替换为 iOS 设备的静止图像。)

4

2 回答 2

2

此 MSDN 文档解释了如何.IsMobileDevicePage_Load. 使其适应您的需求应该是微不足道的。

还要检查这个其他答案

还有51Degrees,一个检测移动设备和浏览器的类库,增强了 .NET 可用的信息。

于 2013-01-13T19:06:39.307 回答
0

在项目中添加一个全局应用类,并将代码写入Session_Start

protected void Session_Start(object sender, EventArgs e)
{

            HttpRequest httpRequest = HttpContext.Current.Request;
            if (httpRequest.Browser.IsMobileDevice)
            {
                string path = httpRequest.Url.PathAndQuery;
               bool isOnMobilePage = path.StartsWith("/Mobile/",
                                                      StringComparison.OrdinalIgnoreCase);
                if (!isOnMobilePage)
                {
                    string redirectTo = "~/Mobile/";

                    // Could also add special logic to redirect from certain 
                    // recognised pages to the mobile equivalents of those 
                    // pages (where they exist). For example,
                    // if (HttpContext.Current.Handler is UserRegistration)
                    //     redirectTo = "~/Mobile/RegistrationMobile.aspx";

                    HttpContext.Current.Response.Redirect(redirectTo);
                }
            }   
        }
于 2014-05-15T08:22:51.297 回答