public class MobileCapableWebFormViewEngine : WebFormViewEngine
{public override ViewEngineResult FindView(ControllerContext controllerContext, string viewName, string masterName, bool useCache){ViewEngineResult result = null;var request = controllerContext.HttpContext.Request;//This could be replaced with a switch statement as other advanced / device specific views are createdif (UserAgentIs(controllerContext, "iPhone")) {result = base.FindView(controllerContext, "Mobile/iPhone/" + viewName, masterName, useCache);}// Avoid unnecessary checks if this device isn't suspected to be a mobile deviceif (request.Browser.IsMobileDevice){if (UserAgentIs(controllerContext, "MSIEMobile 6")){result = base.FindView(controllerContext, "Mobile/MobileIE6/" + viewName, masterName, useCache);}else if (UserAgentIs(controllerContext, "PocketIE") && request.Browser.MajorVersion >= 4){result = base.FindView(controllerContext, "Mobile/PocketIE/" + viewName, masterName, useCache);}//Fall back to default mobile view if no other mobile view has already been setif ((result == null || result.View == null) &&request.Browser.IsMobileDevice){result = base.FindView(controllerContext, "Mobile/" + viewName, masterName, useCache);}}//Fall back to desktop view if no other view has been selectedif (result == null || result.View == null){result = base.FindView(controllerContext, viewName, masterName, useCache);}return result;}private bool UserAgentIs(ControllerContext controllerContext, string userAgentToTest){return (controllerContext.HttpContext.Request.UserAgent.IndexOf(userAgentToTest, StringComparison.OrdinalIgnoreCase) > 0);}
}