2011-08-12 19 views
19

những gì tôi đang cố gắng đạt được là đơn giản; Trong số tất cả các quan điểm mà tôi có trong ứng dụng web của tôi, tôi chỉ có hai quan điểm dao cạo mà tôi đã tạo ra một phiên bản di động cho họ. vì vậy tôi cần phải chuyển hướng người dùng đến các chế độ xem này nếu họ đang truy cập ứng dụng từ thiết bị di động của họ. tôi đã cố gắng sau vào mức độ điều khiển nhưng nó không chuyển hướng người dùng khi tôi chạy thử nghiệm của tôi trên các thiết bị di động khác nhau: -Làm thế nào tôi có thể phát hiện nếu yêu cầu đến từ một trình duyệt di động trong asp.net MVC 3

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

Vậy là có cách tiếp cận khác mà tôi có thể làm theo mà có thể phát hiện hầu hết các thiết bị di động ? Cảm ơn

Trả lời

17

Giả sử chế độ xem di động của bạn dành cho tất cả thiết bị di động (thay vì có chế độ xem dành riêng cho thiết bị), bạn có thể kiểm tra chuỗi tác nhân người dùng để xem chế độ xem nào bạn sẽ quay lại. Đây chỉ là một ví dụ, nhưng sẽ giúp bạn khá xa cùng:

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)); 
} 

Sau đó, trong hành động điều khiển của bạn, bạn có thể gọi:

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

Nếu bạn vẫn thấy nó không nhận ra trình duyệt di động của bạn, kiểm tra chuỗi tác nhân người dùng trong trình gỡ lỗi và xem liệu có số nhận dạng bạn có thể sử dụng hay không.

+0

nhờ trả lời. nhưng bạn nên thêm phương thức "public static bool IsMobileDevice (string userAgent)" vào ứng dụng web MVC của tôi ở đâu? – qalife4ever

+0

Bạn có thể đặt nó trong một lớp trợ giúp (mà tôi đã đặt tên là 'MobileHelper'.) Sau đó bạn có thể gọi phương thức đó từ một hành động trên bộ điều khiển và trả về 'Chế độ xem 'phù hợp tùy thuộc vào điều kiện có đáp ứng điều kiện hay không. – dlev

+0

cảm ơn bạn đã trả lời ur, tôi đã tạo một lớp mới, nhưng khi tôi chạy thử nghiệm, người dùng thiết bị di động không được chuyển hướng đến lượt xem trên thiết bị di động? BR – qalife4ever

24

Bạn có thể sử dụng thuộc tính Request.Browser.IsMobileDevice.

+1

tôi đã thử điều này nhưng nó không phát hiện được trình duyệt di động, vì vậy tôi đã cố cập nhật định nghĩa trình duyệt từ liên kết này http://stephenwalther.com/blog /archive/2010/03/05/use-asp.net-4-browser-definitions-with-asp.net-3.5.aspx ,, nhưng tôi thấy rằng danh sách đã được cập nhật hơn một năm trước !! – qalife4ever

+2

@ qalife4ever: Hãy thử cái này: http://owenbrady.net/browsercaps/OceanMobile.v4.browser.xml Bạn đang thử nghiệm thiết bị nào? – BFree

+0

Thực ra tôi đang thử nghiệm bằng trình giả lập di động opera trên PC cho Nokia E63 và một trình mô phỏng khác cho blackberry ?, tôi e rằng điều này có nghĩa là các yêu cầu sẽ không được phát hiện là yêu cầu di động, nhưng điều này không phải là trường hợp? !! – qalife4ever

12

Tôi sử dụng 51degrees.mobi package from nuget. Điều này là chính xác hơn trong việc phát hiện tất cả các thiết bị di động khác nhau. Nó hoạt động ngay lập tức.

Khi trình duyệt là thiết bị di động, tôi chuyển hướng đến một Khu vực khác.

Tôi cũng khuyên bạn nên đọc Steve Sandersons blog về chủ đề.

+0

Thực sự tốt, chỉ cần những gì tôi cần , tất cả mọi người nên cung cấp cho một thử này, vì vậy dễ dàng để tích hợp bằng cách sử dụng gói nuget - "Install-Package 51Degrees.mobi". Đồng thời xem tài liệu chính thức này giúp bắt đầu rất nhanh - https://51degrees.com/support/documentation/net/getting-started –

1

Sử dụng WURFL http://wurfl.sourceforge.net/dotnet_index.php

Nếu bạn sử dụng asp.net MVC bạn có thể sử dụng một 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); 
     } 
    } 
} 
+0

sẽ không 'is_wireless_device' hoạt động trên bất kỳ thiết bị nào không dây (ví dụ: máy tính xách tay) hoặc là điện thoại chỉ có khả năng nhắm mục tiêu 'GetCapability'? – KDOT

0

tôi sử dụng phương pháp này để phát hiện điện thoại di động và máy tính để bàn

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"); 
} 
0

Bạn có thể sử dụng

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

Sử dụng mã nguồn mở 51Degrees '.Net Api, mà bạn có thể truy cập tại đây, https://github.com/51Degrees/dotNET-Device-Detection, bạn có thể phát hiện rất nhiều loại thiết bị di động.

Bạn có thể làm điều gì đó tương tự như vậy trong tệp 51Degrees.config để bật chuyển hướng.

<redirect devicesFile="" timeout="20" firstRequestOnly="true" 
    originalUrlAsQueryString="false" mobileHomePageUrl="~/Mobile/StudentStartAssessment.aspx" 
    mobilePagesRegex="/Mobile/"> 
    <locations> 
    <clear /> 
    <location name="noredirect" url="" matchExpression="" enabled="true"> 
     <add property="Url" matchExpression="[&amp;|\?]noredirect" enabled="true" /> 
    </location> 
    <location name="Mobile" url="~/Mobile/StudentStartAssessment.aspx" matchExpression="" 
     enabled="true"> 
     <add property="IsMobile" matchExpression="True" enabled="true" /> 
    </location> 
    </locations> 
</redirect> 

Để biết thêm thông tin về vấn đề này, bạn có thể xem tại đây https://51degrees.com/Developers/Documentation/APIs/NET-V32/Web-Apps/Configuration/Redirect

Tiết lộ: Tôi làm việc cho 51Degrees

Các vấn đề liên quan