ASP.NET MVC 4 Mobile Caching Bug Fixed

31 October 2012 Update: The ASP.NET Fall 2012 Update includes the Fixed DisplayModes NuGet package in the new project templates.  You will still need to apply the Fixed DisplayModes NuGet package to projects created before you install the ASP.NET Fall 2012 Update.

The RC and RTM versions of ASP.NET MVC 4 had a bug in the view caching mechanism that resulted in mobile views (and other views specified by inserting a new DefaultDisplayMode)  returning the wrong view.  Initially the correct mobile view would be returned, but when the mobile view expired from the cache (which has a sliding default of 15 minutes) and when the desktop view was still in the cache, a mobile request for the view would return the desktop view. This bug was reported as bug number 280 on CodePlex and in the ASP.NET MVC forum here.

Most applications that hit this problem can simply install the Fixed DisplayModes NuGet package. The ASP.NET Fall 2012 Update includes the Fixed DisplayModes NuGet package in the new project templates.

Applications that customize the collection of registered view engines will need to reference the fixed razor or fixed web forms view engine:

  • Microsoft.Web.Mvc.FixedRazorViewEngine
  • Microsoft.Web.Mvc.FixedWebFormViewEngine

Applications that remove all the view engines, then add back Razor with the following code:

 ViewEngines.Engines.Clear();
ViewEngines.Engines.Add(new RazorViewEngine());
 
 
 
 
 

Will need to replace the above code with the following:

 ViewEngines.Engines.Clear();
ViewEngines.Engines.Add(new Microsoft.Web.Mvc.FixedRazorViewEngine());
 

In addition to blogging, I use Twitter to make quick posts and share links. My Twitter handle is: @RickAndMSFT