Mobile Enabled Razor View Engine

Today I was trying to add jQuery Mobile to my project. To start with I found Scott Hanselman’s blog entry

A Better ASP.NET MVC Mobile Device Capabilities ViewEngine . I used the code by deriving the RazorViewEngine. My class looks like this:

 

WebEnabledViewEngine
  1. public class WebEnabledViewEngine : RazorViewEngine
  2.     {
  3.         public override ViewEngineResult FindView(ControllerContext controllerContext, string viewName, string masterName, bool useCache)
  4.         {
  5.             ViewEngineResult result = null;
  6.             var request = controllerContext.HttpContext.Request;
  7.  
  8.             // Avoid unnecessary checks if this device isn't suspected to be a mobile device
  9.             if (request.Browser.IsMobileDevice)
  10.             {
  11.                 result = base.FindView(controllerContext, "Mobile/" + viewName, masterName, useCache);
  12.             }
  13.  
  14.             //Fall back to desktop view if no other view has been selected
  15.             if (result == null || result.View == null)
  16.             {
  17.                 result = base.FindView(controllerContext, viewName, masterName, useCache);
  18.             }
  19.  
  20.             return result;
  21.         }
  22.     }

 

Apparently Scott has marked his blog entry as broken. The issue I was facing was that my jQuery Mobile site  was running good but the desktop version failed to load script files. So I end up adding following routing entry and now both version work’s good.

 

Global.asax.cs
  1. routes.IgnoreRoute("{*alljs}", new { alljs = @".*\.js(/.*)?" });

Advertisements