【问题标题】:Use ASP.Net MVC only for mobile clients仅对移动客户端使用 ASP.Net MVC
【发布时间】:2013-02-28 01:25:21
【问题描述】:

我们目前有一个大型的 ASP.Net webforms 网站,我们刚刚开始将其中的一些部分转换为 asp.net MVc

我们想专门为移动客户端创建一个网站版本,并且我们想开始使用 MVC。

我知道 MVC 按照惯例支持为移动客户端提供以 .mobile 结尾的视图文件。但我的问题是 - 我一般如何设置桌面客户端仍然使用 default.aspx 但移动客户端被重定向到 /home

【问题讨论】:

    标签: c# mobile asp.net-mvc-4


    【解决方案1】:

    浏览器检测代码应该放在一个基类中,您的所有页面都将从该基类中派生。试试下面的代码:

    protected void Page_Load(object sender, EventArgs e)
    {
        if (IsMobileBrowser())
        {
             Response.Redirect("/home");    
             return;      
        }
    
        // your other code
    } 
    
    public static bool IsMobileBrowser()
    {
        //GETS THE CURRENT USER CONTEXT
        HttpContext context = HttpContext.Current;
    
        //FIRST TRY BUILT IN ASP.NT CHECK
        if (context.Request.Browser.IsMobileDevice)
        {
            return true;
        }
        //THEN TRY CHECKING FOR THE HTTP_X_WAP_PROFILE HEADER
        if (context.Request.ServerVariables["HTTP_X_WAP_PROFILE"] != null)
        {
            return true;
        }
        //THEN TRY CHECKING THAT HTTP_ACCEPT EXISTS AND CONTAINS WAP
        if (context.Request.ServerVariables["HTTP_ACCEPT"] != null && 
            context.Request.ServerVariables["HTTP_ACCEPT"].ToLower().Contains("wap"))
        {
            return true;
        }
        //AND FINALLY CHECK THE HTTP_USER_AGENT 
        //HEADER VARIABLE FOR ANY ONE OF THE FOLLOWING
        if (context.Request.ServerVariables["HTTP_USER_AGENT"] != null)
        {
            //Create a list of all mobile types
            string[] mobiles =new[]
            {
                  "midp", "j2me", "avant", "docomo", 
                  "novarra", "palmos", "palmsource", 
                  "240x320", "opwv", "chtml",
                  "pda", "windows ce", "mmp/", 
                  "blackberry", "mib/", "symbian", 
                  "wireless", "nokia", "hand", "mobi",
                  "phone", "cdm", "up.b", "audio", 
                  "SIE-", "SEC-", "samsung", "HTC", 
                  "mot-", "mitsu", "sagem", "sony"
                  , "alcatel", "lg", "eric", "vx", 
                 "NEC", "philips", "mmm", "xx", 
                 "panasonic", "sharp", "wap", "sch",
                 "rover", "pocket", "benq", "java", 
                 "pt", "pg", "vox", "amoi", 
                 "bird", "compal", "kg", "voda",
                 "sany", "kdd", "dbt", "sendo", 
                 "sgh", "gradi", "jb", "dddi", 
                 "moto", "iphone"
            };
    
            //Loop through each item in the list created above 
            //and check if the header contains that text
            foreach (string s in mobiles)
            {
                if(context.Request.ServerVariables["HTTP_USER_AGENT"].ToLower().Contains(s.ToLower()))
                {
                    return true;
                }
            }
        }
    
        return false;
    }
    

    查看更多:

    http://www.codeproject.com/Articles/213825/ASP-net-Mobile-device-detection

    http://www.codeproject.com/Articles/34422/Detecting-a-mobile-browser-in-ASP-NET

    【讨论】:

    • 我将使用这种方法,但我将使用 51Degrees 移动设备数据库
    【解决方案2】:

    这样试试

    if (request.Browser.IsMobileDevice)
    {
     //rediret the user to mobile views
    }
    

    确保删除 webform View Engine。像这样。 MobileCompatibleViewEngine

    ViewEngines.Engines.Remove(ViewEngines.Engines.OfType<WebFormViewEngine>().First());
    ViewEngines.Engines.Add(new MobileCapableWebFormViewEngine()); // this will come from Nuget Package. 
    

    【讨论】:

      【解决方案3】:

      将以下代码写入 Global.asax。

      如果用户将通过桌面访问该网站,它将自动重定向到桌面网站天气用户通过 Android 手机访问它会 自动重定向到移动网站

       protected void Application_Start()
          {
              DisplayModes.Modes.Insert(0, new DefaultDisplayMode("Android")
              {
                  ContextCondition = (context => context.Request.UserAgent.IndexOf
                      ("Android", StringComparison.OrdinalIgnoreCase) >= 0)
              });
      
              DisplayModes.Modes.Insert(0, new DefaultDisplayMode("WindowsPhone")
              {
                  ContextCondition = (context => context.Request.UserAgent.IndexOf
                      ("Windows Phone OS", StringComparison.OrdinalIgnoreCase) >= 0)
              });
          }
      

      【讨论】:

      • 这只有在启动页面是主控制器时才有效 - 而不是 aspx 页面
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2010-09-14
      • 2014-07-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多