使用ASP.Net MVC仅适用于移动客户端

本文关键字:适用于 移动 客户端 MVC ASP Net 使用 | 更新日期: 2023-09-27 18:01:24

我们目前有一个大型ASP。Net webforms网站,我们刚开始把它的一些部分转换成asp.net MVc

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

我知道MVC按照约定支持以.mobile结尾的视图文件是为移动客户端服务的。但我的问题是——我该如何设置桌面客户端仍然使用默认值。但是移动客户端会被重定向到/home

使用ASP.Net MVC仅适用于移动客户端

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

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

试试

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

确保你删除了webform视图引擎。像这样。MobileCompatibleViewEngine

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

将以下代码写入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)
        });
    }