DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
  1. DZone
  2. Coding
  3. Frameworks
  4. Switching between mobile and desktop hosts (ASP.NET MVC 4)

Switching between mobile and desktop hosts (ASP.NET MVC 4)

Imran Baloch user avatar by
Imran Baloch
·
Mar. 11, 12 · Interview
Like (0)
Save
Tweet
Share
8.43K Views

Join the DZone community and get the full member experience.

Join For Free

Introduction:

ASP.NET MVC 4 comes with a bunch of new features including lots 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 hosts for mobile and desktop version of the site, because major websites already use a different host name, depending on the type of the invoking client. For example, twitter.com uses mobile.twitter.com for mobile and facebook.com uses m.facebook.com. 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.

ASP.NET MVC Host (Unix) mobile app ASP.NET Desktop (word processor)

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Required Knowledge To Pass AWS Certified Solutions Architect — Professional Exam
  • 10 Best Ways to Level Up as a Developer
  • Is DevOps Dead?
  • What Is API-First?

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: