Thoughts from Dan Miser RSS 2.0
# Thursday, 08 November 2012
The new DisplayMode engine in ASP.NET MVC 4 is rather nice. By default, it will detect whether you come in from a mobile device or a desktop machine, and load up a specialized view for you based on that detection.

For example, if you were trying to navigate to from an iPhone, the default behavior will be to look for Players\Index.Mobile.cshtml, followed by Players\Index.cshtml. Whichever it finds first will be the view that gets used. Note that this also requires you to have a _Layout.Mobile.cshtml file.

This is great for new projects, but I have a non-trivial app with a lot of views that have been built over the years using the technique by Scott Hanselman. That approach would look for the file in Players\Mobile\Index.cshtml. I was not looking forward to renaming all of those files.

In order to use my already existing file structure, I added one class:

public class SubfolderDisplayMode : DefaultDisplayMode
    public SubfolderDisplayMode() : base("Mobile")
        // Be sure to use, or a good browser capabilities file
        ContextCondition = (context => context.GetOverriddenBrowser().IsMobileDevice);

    protected override string TransformPath(string virtualPath, string suffix)
        var dir = VirtualPathUtility.GetDirectory(virtualPath);
        var filename = VirtualPathUtility.GetFileName(virtualPath);
        return VirtualPathUtility.Combine(dir, "Mobile/" + filename);

Registered it in my Globabl.asax.cs:

DisplayModeProvider.Instance.Modes.Insert(0, new SubfolderDisplayMode());

It works very well, and I can come back and rename those files later.

Also be sure to put a call to @Html.Partial("_VewSwitcher") somewhere in your desktop _Layou.cshtml file so the user can get back to the mobile version of the site.

Updated on 11/9/12 to clean the sample code up a bit
Thursday, 08 November 2012 21:11:20 (GMT Standard Time, UTC+00:00)  #    Comments [0] -
Comments are closed.
<2018 March>
About the author/Disclaimer

The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.

© Copyright 2018
Dan Miser
Sign In
Total Posts: 388
This Year: 0
This Month: 0
This Week: 0
Comments: 630
Pick a theme:
All Content © 2018, Dan Miser
DasBlog theme 'Business' created by Christoph De Baene (delarou)