Switching between Mobile and Desktop Host in ASP.NET MVC 4

        Introduction:


                    ASP.NET MVC 4 comes with a bunch of new features including lot of features for mobile. Mobile features include Mobile Optimized Project Templates, Display Modes, View Switcher, Browser Overriding, etc. In addition to these features, you may need to use different host for mobile and desktop because lot of sites uses a different host name for mobile and desktop. For example, twitter.com uses mobile.twitter.com host name for mobile and facebook.com uses m.facebook.com host name for mobile. In this article, I will show you how to use different host name for desktop and mobile.

 


        Description:


                    First of all you need create a new ASP.NET MVC 4 application. Then, create a new RequestSwitcherAttribute.cs file and add the following lines in this file,


        public class RequestSwitcherAttribute : ActionFilterAttribute
        {
            string desktopHost, mobileHost;
            public RequestSwitcherAttribute(string desktopHost, string mobileHost)
            {
                if (string.IsNullOrWhiteSpace(desktopHost) || string.IsNullOrWhiteSpace(mobileHost))
                    throw new ArgumentNullException("Host or MobileHost");
                this.desktopHost = desktopHost;
                this.mobileHost = mobileHost;
            }
            public override void OnActionExecuting(ActionExecutingContext filterContext)
            {
                var context = filterContext.HttpContext;
                var request = context.Request;
                var port = request.Url.Port;
                var host = request.UserHostName;
                var device = context.GetOverriddenBrowser();
                if ((device.IsMobileDevice && host != mobileHost) || (!device.IsMobileDevice && host != desktopHost))
                {
                    string url = (request.IsSecureConnection ? "https://" : "http://") +
                                 (device.IsMobileDevice ? mobileHost : desktopHost) +
                                 ((port == 80 || port == 443)  ? "" : ":" + port) + request.RawUrl;
                    filterContext.Result = new RedirectResult(url);
                }
            }
        }

                    This is an action filter which simply checks whether the request is coming from desktop or mobile. Then, it will compare the site's mobile/desktop host name with the current request host name. The filter will redirect the user to mobile/desktop host if the current request host name does not match with mobile/desktop host name.


                    Now just open global.asax.cs file and register this filter globally,


        public static void RegisterGlobalFilters(GlobalFilterCollection filters)
        {
            filters.Add(new HandleErrorAttribute());
            filters.Add(new RequestSwitcherAttribute("::1", "127.0.0.1"));
        }

 


                    Note that for testing purpose, I have used ::1(localhost, loopback address) host name for desktop and 127.0.0.1 host name for mobile. Feel free to replace it with your actual host name. When doing so, you need to map your desktop and mobile host dns name.


        Summary:


                    Lot of sites today uses different host name for desktop and mobile. In this article, I showed you how to let the mobile user to use mobile host and desktop user to use desktop host. Hopefully you will enjoy this article too.


Published Saturday, March 10, 2012 12:25 PM by imran_ku07
Filed under: , ,

Comments

# Switching between Mobile and Desktop Host in ASP.NET MVC 4 - Imran Baloch's Blog | asp.net mvc | Scoop.it

Pingback from  Switching between Mobile and Desktop Host in ASP.NET MVC 4 - Imran Baloch's Blog | asp.net mvc | Scoop.it

# re: Switching between Mobile and Desktop Host in ASP.NET MVC 4

Saturday, March 10, 2012 10:47 PM by leia

Awesome, thanks for this.

# Switching between Mobile and Desktop Host in... | ASP.NET | Syngu

Pingback from  Switching between Mobile and Desktop Host in... | ASP.NET | Syngu

# re: Switching between Mobile and Desktop Host in ASP.NET MVC 4

Sunday, March 11, 2012 5:44 AM by Hoogoo

Exactly what i was looking for.

# ASP.NET Mobile development – Useful links | abaBiaS Inc

Saturday, July 14, 2012 11:07 AM by ASP.NET Mobile development – Useful links | abaBiaS Inc

Pingback from  ASP.NET Mobile development – Useful links | abaBiaS Inc