Programming
Article
By Malcolm Sheridan

ASP.NET MVC 4 Developer Preview: Display Modes

By Malcolm Sheridan
Help us help you! You'll get a... FREE 6-Month Subscription to SitePoint Premium Plus you'll go in the draw to WIN a new Macbook SitePoint 2017 Survey Yes, let's Do this It only takes 5 min

This is the second article focusing on the new additions to ASP.NET MVC 4.  Today’s article will focus on using Display Modes.  This selects a view depending on the browser making the request, which means you can target specific devices and give the user a truly rich experience.

Before getting started, you should read the first article in this series on ASP.NET MVC 4 Developer Preview.

Installation

Before undertaking any development, you’ll need to install the MVC 4 builds. The simplest way to do this is via the Web Platform Installer. MVC 4 is available for Visual Studio 2010 or Visual Studio 2011 Developer Preview.

All of the MVC articles I’m authoring are developed in Visual Studio 2011 Developer Preview. Below are the links to get started.

Default Mobile Views in MVC 4

It’s important to understand a new feature in MVC 4.  By default, if you add a .mobile.cshtml view to a folder, that view will be rendered by mobile and tablet devices.

fig1

This is a nice feature, but if you want to target specific devices, such as the iPhone, iPad or Windows Phone, you can use Display Modes.

To do this, you need to register a new DefaultDisplayMode instance to specify which name to search for when a request meets the condition.  You set this in the global.asax file in the Application_Start event.  Here’s how to specify a display mode for an iPhone.

protected void Application_Start()

{

DisplayModes.Modes.Insert(0, new DefaultDisplayMode("iPhone")

{

ContextCondition = (context =>context.Request.UserAgent.IndexOf

("iPhone", StringComparison.OrdinalIgnoreCase) >= 0)

});

}

To consume views that meet this condition, you create a view but change the extension to .iPhone.cshtml.  Likewise if you want to target an iPad, create an iPad instance.

DisplayModes.Modes.Insert(0, new DefaultDisplayMode("iPad")

{

ContextCondition = (context =>context.Request.UserAgent.IndexOf

("iPad", StringComparison.OrdinalIgnoreCase) >= 0)

});

Basically, Display Modes checks the User Agent.  If it finds a matching suffix, it will display any view it finds that matches the suffix.  The suffix is the parameter that’s passed to the DefaultDisplayMode method.  To see this in action, I’ve created a Home controller and added three views to the Home folder.

fig2

The differences between the views is the H1 heading.  They’ll display iPhone, iPad or Desktop depending on the device.  I’m also displaying the User Agent so you can see it changing.  First I’ll debug the website through my desktop browser.  You can  see the desktop specific page being served.

fig3

Now navigate to the website using an iPhone.  You’ll see the iPhone specific page being served.

fig4

Switching over to an iPad, you’ll see the iPad specific page being served.

fig5

This is a simple way to target specific devices, producing a view that suits the device – and thus the user.

Testing with Emulators

To test these new features, you can either use a physical iPhone or iPad, or you can use an emulator.  The emulator I was using is from MobiOne.  You can download a 15 day free trial here.

The Windows Phone RC emulator is free and can be downloaded here.

Likewise another good option is the User Agent Switcher add-on for Firefox, which changes the user agent that’s sent to the browser.  That can be downloaded here.

Login or Create Account to Comment
Login Create Account
Recommended
Sponsors
Get the most important and interesting stories in tech. Straight to your inbox, daily.Is it good?