如何检测请求是否来自我的asp.net MVC 3中的移动浏览器

我想要实现的是简单的; 在我的Web应用程序中的所有视图中,我只有两个剃刀视图,我为他们创建了一个移动版本。 因此,如果他们从移动设备访问应用程序,我需要将用户重定向到这些视图。 我在控制器级别上尝试了以下操作,但是当我在不同的移动设备上运行测试时,它没有重定向用户: –

if (Request.Browser.IsMobileDevice) { return View("MobileStudentStartAssessment"); } else { return View("StudentStartAssessment"); } 

那么我可以采用另一种可以检测大多数移动设备的方法吗? 谢谢

您可以使用Request.Browser.IsMobileDevice属性。

假设您的移动视图适用于所有移动设备(而不是具有特定于设备的视图),您可以检查用户代理字符串以查看应返回的视图。 这只是一个例子,但应该让你走得很远:

 private static string[] mobileDevices = new string[] {"iphone","ppc", "windows ce","blackberry", "opera mini","mobile","palm", "portable","opera mobi" }; public static bool IsMobileDevice(string userAgent) { // TODO: null check userAgent = userAgent.ToLower(); return mobileDevices.Any(x => userAgent.Contains(x)); } 

然后,在您的控制器操作中,您可以调用:

 if (MobileHelper.IsMobileDevice(Request.UserAgent)) { // Return mobile view } 

如果您仍然发现它无法识别您的移动浏览器,请检查调试器中的用户代理字符串,看看是否有可以使用的标识符。

我使用nuget的51degrees.mobi包 。 这在检测所有不同的移动设备时更准确。 它马上工作了。

当浏览器是移动设备时,我将其重定向到其他区域。

我还建议阅读史蒂夫桑德森博客的主题。

使用WURFL http://wurfl.sourceforge.net/dotnet_index.php

如果你使用asp.net mvc,你可以使用ActionFilter

 public class MobileActionFilterAttribute : ActionFilterAttribute { // The WURFL database contains information about a huge number of devices and mobile browsers. // http://wurfl.sourceforge.net/ // http://wurfl.sourceforge.net/dotnet_index.php // http://wurfl.sourceforge.net/help_doc.php private static readonly IWURFLManager WurflManager; static MobileActionFilterAttribute () { IWURFLConfigurer configurer = new ApplicationConfigurer(); WurflManager = WURFLManagerBuilder.Build(configurer); } public override void OnActionExecuting(ActionExecutingContext filterContext) { HttpRequestBase request = filterContext.RequestContext.HttpContext.Request; // We don't have ARR server for dev environment, so we still need to check to see if the current domain name is the mobile site. if (request.Url.AbsoluteUri.StartsWith(SiteConfiguration.Current.MobileSiteAddress, StringComparison.OrdinalIgnoreCase)) { return; } // Creates a WURFLRequest object from an ASP.NET HttpRequest object WURFLRequest wurflRequest = WURFLRequestFactory.CreateRequest(HttpContext.Current.Request); // Indicates whether the current user agent string refers to a desktop agent. if (wurflRequest.IsDesktopRequest) return; // Get the information about the device IDevice deviceInfo = WurflManager.GetDeviceForRequest(wurflRequest); // Tells you if a device is a tablet computer (iPad and similar, regardless of OS) bool isTablet = string.Equals(deviceInfo.GetCapability("is_tablet") ?? string.Empty, "true", StringComparison.OrdinalIgnoreCase); if (isTablet) { // so we don't show the mobile site for iPad. return; } // Indicates whether the current user agent string refers to a mobile device. bool isMobileRequest = wurflRequest.IsMobileRequest; // Tells you if a device is wireless or not. Specifically a mobile phone or a PDA are considered wireless devices, a desktop PC or a laptop are not bool isWirelessDevice = string.Equals(deviceInfo.GetCapability("is_wireless_device") ?? string.Empty, "true", StringComparison.InvariantCultureIgnoreCase); if (isMobileRequest && isWirelessDevice) { // we can redirect to the mobile site! filterContext.Result = new RedirectResult(SiteConfiguration.Current.MobileSiteAddress); } } } 

我使用这种方法来检测移动和桌面

 if (eDurar.MobileDetect.DeviceType.Any(m => Request.UserAgent.Contains(m))) { Layout = "~/Views/Shared/_mobileLayout.cshtml"; @Html.Partial("mobileIndex"); } else { Layout = "~/Views/Shared/_Layout.cshtml"; @Html.Partial("desktopIndex"); } 

您可以使用

  if (Request.Browser["IsMobileDevice"] == "true") { //Mobile Browser Detected } else { //Desktop Browser Detected } 

使用51Degrees的开源.Net Api,你可以到这里, https://github.com/51Degrees/dotNET-Device-Detection ,你可以检测到各种各样的移动设备。

您可以在51Degrees.config文件中执行与此类似的操作以启用重定向。

            

有关这方面的更多信息,请访问https://51degrees.com/Developers/Documentation/APIs/NET-V32/Web-Apps/Configuration/Redirect

披露:我为51Degrees工作