2011-08-12 24 views
19

我想實現的是簡單的;在我的Web應用程序中所有的視圖中,我只有兩個剃刀視圖,我爲它們創建了一個移動版本。 所以我需要將用戶重定向到這些視圖,如果他們從他們的移動設備訪問應用程序。我試圖在控制水平以下,但是當我跑我在不同的移動設備測試沒有重定向用戶: -我如何檢測請求是否來自我的asp.net MVC中的移動瀏覽器3

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

那麼,有沒有我可以跟隨它可以檢測出大部分移動設備的另一種方法? 謝謝

回答

17

假設您的移動視圖適用於所有移動設備(而不是設備特定的視圖),您可以檢查用戶代理字符串以查看應該返回哪個視圖。這只是一個例子,但應該讓你很遠沿着:

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

然後,在你的控制器動作,您可以撥打:

​​3210

如果你還覺得它沒有認識到你的手機瀏覽器,檢查調試器中的用戶代理字符串,看看是否有可以使用的標識符。

+0

感謝您的答覆工作。但建議在我的MVC Web應用程序中添加「public static bool IsMobileDevice(string userAgent)」方法? – qalife4ever

+0

你可以把它放在一個助手類(我命名爲「MobileHelper」)中。)然後,您可以從控制器上的操作調用該方法,並根據是否滿足條件返回相應的「視圖」。 – dlev

+0

感謝您的回覆,我創建了一個新課程,但是當我運行我的測試時,移動用戶沒有重定向到移動視圖? BR – qalife4ever

24

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

+1

我試過這個,但它沒有檢測到移動瀏覽器,,所以我試圖從這個鏈接更新瀏覽器定義http://stephenwalther.com/blog /archive/2010/03/05/use-asp.net-4-browser-definitions-with-asp.net-3.5.aspx,但我發現列表更新超過一年前! – qalife4ever

+2

@ qalife4ever:試試這個:http://owenbrady.net/browsercaps/OceanMobile.v4.browser.xml你正在測試哪個設備? – BFree

+0

其實我正在使用我的PC上的Opera移動模擬器來測試諾基亞E63和另一個黑莓模擬器嗎?,我恐怕這意味着請求不會被檢測爲移動請求,但這不應該是案件?!! – qalife4ever

12

我使用51degrees.mobi package from nuget。這在檢測所有不同的移動設備時更準確。它馬上工作。

當瀏覽器是移動設備時,我將其重定向到不同的區域。

我也推薦閱讀Steve Sandersons blog這個話題。

+0

真的很好,正是我需要的,每個人都應該嘗試一下,這樣使用nuget軟件包 - 「Install-Package 51Degrees.mobi」很容易整合。另請參閱此官方文檔有助於快速入門 - https://51degrees.com/support/documentation/net/getting-started –

1

使用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); 
     } 
    } 
} 
+0

不會對任何無線設備(即:筆記本電腦)執行「is_wireless_device」操作或是'GetCapability'已經只針對手機? – KDOT

0

我用這個方法來檢測移動和桌面

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

您可以使用

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

使用51Degrees的開源.Net Api,您可以在這裏獲得https://github.com/51Degrees/dotNET-Device-Detection,您可以檢測到各種各樣的移動設備。

您可以在51Degrees.config文件中做類似於此的操作來啓用重定向。

<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> 

有關它的更多信息,你可以看看這裏https://51degrees.com/Developers/Documentation/APIs/NET-V32/Web-Apps/Configuration/Redirect

披露:我爲51Degrees

相關問題