【问题标题】:ASP.NET MVC Check is mobile in Global.asaxASP.NET MVC Check 在 Global.asax 中是可移动的
【发布时间】:2019-03-09 21:47:53
【问题描述】:

我想检查用户是否在 Global.asax 中的 Application_Start 函数内的移动页面上,以便创建移动显示模式。

有什么办法可以做到吗? 我厌倦了 Request.Browser.IsMobileDevice 但我得到了异常“请求在此上下文中不可用”

【问题讨论】:

  • Application_Start 在首次启动 ASP.NET 应用程序时触发。它不会被用户的每个请求触发(因此无法访问Request),因此它不是处理用户特定/请求特定操作的好选择

标签: c# asp.net asp.net-mvc iis global-asax


【解决方案1】:

使用Application_BeginRequest 代替也使用HTTP_USER_AGENT 服务器变量代替 Request.IsMobileDevice :

 void Application_BeginRequest(object sender, EventArgs e)
    {
       var u = Request.ServerVariables("HTTP_USER_AGENT");
       var uri =  Request.Url.AbsoluteUri.ToLower();
          //put DetectMobileBrowsersCode Here, for exmaple if user agents contains apple , android , etc ...

          if (b.IsMatch(u) || v.IsMatch(Left(u, 4)))
          {
              Response.Redirect("http://m.yoursite.com");
          } 
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-08-02
    • 2012-09-17
    • 1970-01-01
    • 1970-01-01
    • 2010-10-20
    • 2010-11-14
    • 2016-02-25
    • 2011-06-24
    相关资源
    最近更新 更多