Exemplo n.º 1
0
        // Ref: http://wurfl.sourceforge.net/dotnet_index.php
        public static IBrowserCaps GetBrowserCaps(HttpContextBase httpContext)
        {
            BrowserCaps browserCaps;

            // We do not need to requery the WURFL manager if we have already
            // cached the BrowserCaps on the HttpContextBase for this instance.
            if (httpContext.Items.Contains(BrowserCapsKeyName))
            {
                browserCaps = httpContext.Items[BrowserCapsKeyName] as BrowserCaps;
                if (browserCaps != null)
                {
                    return(browserCaps);
                }
            }


            // Otherwise, query the WURFL manager for this HttpRequest.
            Log.Debug("Start: WURFL Query");
            var  device = WurflManager.GetDeviceForRequest(httpContext.Request.UserAgent ?? String.Empty);
            bool isMobile, isTablet, hasCookieSupport;

            Boolean.TryParse(device.GetCapability("is_wireless_device"), out isMobile);
            Boolean.TryParse(device.GetCapability("is_tablet"), out isTablet);
            Boolean.TryParse(device.GetCapability("cookie_support"), out hasCookieSupport);

            browserCaps = new BrowserCaps
            {
                IsMobile         = isMobile,
                IsTablet         = isTablet,
                IsDesktop        = !(isMobile || isTablet),
                HasCookieSupport = hasCookieSupport,
                BrandName        = device.GetCapability("brand_name"),
                ModelName        = device.GetCapability("model_name"),
                DeviceOs         = device.GetCapability("device_os")
            };

            Log.Debug("Stop: WURFL Query");
            return(browserCaps);
        }
Exemplo n.º 2
0
    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);
        }
    }