【问题标题】:How to redirect to mobile page when its mobile or tablet?当它的手机或平板电脑时如何重定向到手机页面?
【发布时间】:2023-03-12 14:48:01
【问题描述】:

我在我的网站中使用 aspx 页面。当用户在手机中打开我的Desktop Website 时,我想重定向到我的Mobile Website。我正在使用 C#。

【问题讨论】:

  • 您的移动链接显示一个空白页面。
  • 您可以实现重定向,例如通过返回 http 标头“位置”来响应请求。
  • 桌面版网站到移动网站的链接返回 404 错误。
  • 很抱歉,我的移动页面正在建设中。

标签: c# html asp.net redirect


【解决方案1】:

您可以使用此处解释的框架检查Request.Browser["IsMobileDevice"] == "true"

http://msdn.microsoft.com/en-us/library/fhhycabe%28v=vs.90%29.aspx

或者您可以使用此处显示的 51Degrees.mobi:

http://51degrees.mobi/

可以在这里找到一个很好的比较:

http://dotnetslackers.com/articles/aspnet/Mobile-Device-Detection-and-Redirection-Using-ASP-NET.aspx

【讨论】:

    【解决方案2】:

    您可以在 C# 中使用 Request.UserAgent 获取 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

    【讨论】:

      【解决方案3】:

      如果使用没有 MasterPage 的简单页面,该页面使用 Request.Browser.IsMobileDevice(或其他方法)来确定移动设备与否,然后重定向到移动设备或桌面设备的相应登录页面?

      我也在 URL 中使用查询字符串:

      ForceMobile = Request.QueryString["ForceMobile"];
      //
      if (Request.Browser.IsMobileDevice || ForceMobile == "Yes")
      {
          Response.Redirect("~/_Mobile/mLogOn.aspx", false);
      }
      else
      {
          Response.Redirect("~/LogOn.aspx", false);
      }
      

      这样做的好处是易于调试,并且不会因直接在登录页面中重定向而导致回发事件。

      【讨论】:

        猜你喜欢
        • 2014-03-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-05-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多