How can i detect if the request is coming from a mobile browser in my asp.net MVC 3

前端 未结 10 1945
旧时难觅i
旧时难觅i 2020-12-14 08:11

what i am trying to achieve is simple; Among all the view which i have in my web application, i have only two razor views that i have created a mobile version for them. so i

相关标签:
10条回答
  • 2020-12-14 09:07

    Using 51Degrees' Open source .Net Api, which you can get here, https://github.com/51Degrees/dotNET-Device-Detection, you can detect a huge variety of mobile devices.

    You can do something similar to this in the 51Degrees.config file to enable redirect.

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

    For more information on this you can look here https://51degrees.com/Developers/Documentation/APIs/NET-V32/Web-Apps/Configuration/Redirect

    Disclosure: I work for 51Degrees

    0 讨论(0)
  • 2020-12-14 09:09

    Use WURFL http://wurfl.sourceforge.net/dotnet_index.php

    If you using asp.net mvc you can use an 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 讨论(0)
  • 2020-12-14 09:10

    I use this method to detect mobile and desktop

    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 讨论(0)
  • 2020-12-14 09:15

    To check the request is from Mobile device or not you can use

    HttpContext.Request.Browser.IsMobileDevice
    

    as this will return a boolean result.

    0 讨论(0)
提交回复
热议问题