If you’re not using (yet) ASP.NET MVC4 (Preview), please read this post and install MVC4.
In this post, I will show you three new functionalities brought by MVC4 for mobile websites.
- The mobile Application Template
- The View Switcher
- The Display mode by Browser type
Smartphone and tablet are able to read websites not optimized for tiny screen but if a user can jump automatically on an optimized version of the website, it’s better for everyone!
One interesting feature of this new version of the ASP.NET MVC framework 4 is the new Mobile Application template. With this template you’ll be able to develop quickly a website especially for mobile and tablet.
1 – Create a full-mobile Website
In Visual Studio, Create a new MVC4 project and select the “Mobile Application” template.

Mobile Application Template - MVC4
I consider that you’ve already develop a classic MVC application. If true, you will not be surprised by the generated project. It’s almost the same as a classic MVC desktop website.
So what the difference?

In the “content” folder you will find another JavaScript library: jQuery Mobile. ASP.NET MVC4 Mobile Template is based on the famous JavaScript framework for mobile application. You can learn a lot if you visit the jQuery mobile website.
Models and Controller are similar to a classic MVC Application.
In the view, you just have to add some tag to tell how jQuery mobile needs to display the page.
If we take a look at this code (Menu of the website generated by MVC4), you will probably recognize the Razor syntax. Nothing change when you want to develop Mobile Application with MVC4. You just have to use special attribute in your HTML.
Here, we declare a classic HTML list….and jQuery Mobile will transform it into an accessible list for mobile devices user.
<ul data-role="listview" data-inset="true">
<li data-role="list-divider">Navigation</li>
<li>@Html.ActionLink("About", "About", "Home")</li>
<li>@Html.ActionLink("Contact", "Contact", "Home")</li>
</ul>

The mobile version of ... Coding-in.net !
This new template is perfect to start learning ASP.NET MVC4 mobile functionnalities and JQuery Mobile. If you already worked with MVC 3, you will see a big different between generated websites ! The MVC 4 website is simple, clean and uses jQuery. It’s a very good point to start learning !
2 – Classic and mobile Website
I will not describe all the new mobile features in MVC 4 in this post but there’s two another interesting features that I want to talk about and wich can interest some of you. It’s the “Display mode by Browser type” and the “View Switcher”.
“View Switcher”
A lot of mobile website have a special link to switch between mobile and classic view. ASP.NET MVC4 allows you to do this in … 1 click !
This browser overriding ability allows user with mobile device to switch onto the classic version and vice versa.
To add this functionnality to your ASP.NET MVC4 website, just open the Package Manager Console (Visual Studio > Tools > Library Package Manager) and paste this line :
Install-Package jQuery.Mobile.MVC

NuGet is awesome isn’t it ? Your solution is updated and you now have a beautiful view switcher in your website !

If we take a look at the added code, you should have a new partial View (in the Shared folder) called _ViewSwitcher.cshtml.
@if (Request.Browser.IsMobileDevice && Request.HttpMethod == "GET")
{
<div class="view-switcher ui-bar-a">
@if (ViewContext.HttpContext.GetOverriddenBrowser().IsMobileDevice)
{
@: Displaying mobile view
@Html.ActionLink("Desktop view", "SwitchView", "ViewSwitcher", new { mobile = false, returnUrl = Request.Url.PathAndQuery }, new { rel = "external" })
}
else
{
@: Displaying desktop view
@Html.ActionLink("Mobile view", "SwitchView", "ViewSwitcher", new { mobile = true, returnUrl = Request.Url.PathAndQuery }, new { rel = "external" })
}
</div>
}
In this code we can see the new GetOverridenBrowser method which returns the request’s user agent override value, or the actual user agent string if no override has been specified.
The view call the SwitchView action in the ViewSwitcher controller.
public RedirectResult SwitchView(bool mobile, string returnUrl) {
if (Request.Browser.IsMobileDevice == mobile)
HttpContext.ClearOverriddenBrowser();
else
HttpContext.SetOverriddenBrowser(mobile ? BrowserOverride.Mobile : BrowserOverride.Desktop);
return Redirect(returnUrl);
}
Here again we could take a look at some new method wich allows overriding brower agent value.
“Display mode by Browser type”
Okay, that is cool. You are now able to develop a full mobile website … But how to switch automatically between classic and mobile website depending on the browser type ? Thanks to MVC 4 , everything can be done easily !
For example, if you already developped a classic website, you can easily develop the Mobile version with a new view called by the same name and with the extension .mobile.cshtml.
If you have a Home.cshtml, create a Home.mobile.cshtml. If a user tries to acces to your website, the mobile version will be displayed.
Better, you can override this feature and develop particular pages for a specific device. Thanks to the User Agent name available in the page request, you can, for example, target an iPhone, Android or WP7 user.
In the Global.asax file, in the Application_Start method, just copy the code below.
DisplayModes.Modes.Insert(0, new DefaultDisplayMode("Android")
{
ContextCondition = (context => context.Request.UserAgent.IndexOf
("Android", StringComparison.OrdinalIgnoreCase) >= 0)
});
DisplayModes.Modes.Insert(0, new DefaultDisplayMode("WindowsPhone")
{
ContextCondition = (context => context.Request.UserAgent.IndexOf
("Windows Phone OS", StringComparison.OrdinalIgnoreCase) >= 0)
});
Here, we are saying to MVC 4 that we will add particular views (With .Android.cshtml and .WindowsPhone.cshtml) to the solution. If the user Agent is declared as an Android or Windows Phone browser, MVC4 will try to find the good view for this device. Simple and efficient !

If you want to change the user agent without buy every mobile on the market, you could use amazing extension like User Agent Switcher (For Firefox).
Okay that’s all for today ! I hope you will enjoy as me these new functionnalities.
Stay tuned of Coding-in.net, I will publish some post about MVC4 !

Canadian Museum of Civilization








