public
anonymous / customviewengine.cs
Created

CustomViewEngine

  • Download Gist
customviewengine.cs
C#
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
 
namespace demo2
{
/// <summary>
/// From Scott Hanselman
/// http://www.hanselman.com/blog/ABetterASPNETMVCMobileDeviceCapabilitiesViewEngine.aspx
/// </summary>
public class CustomViewEngine : IViewEngine
{
public IViewEngine BaseViewEngine { get; private set; }
public Func<ControllerContext, bool> IsTheRightDevice { get; private set; }
public string PathToSearch { get; private set; }
 
public CustomViewEngine(Func<ControllerContext, bool> isTheRightDevice, string pathToSearch, IViewEngine baseViewEngine)
{
BaseViewEngine = baseViewEngine;
IsTheRightDevice = isTheRightDevice;
PathToSearch = pathToSearch;
}
 
public ViewEngineResult FindPartialView(ControllerContext context, string viewName, bool useCache)
{
if (IsTheRightDevice(context))
{
return BaseViewEngine.FindPartialView(context, PathToSearch + "/" + viewName, false);//switching to useCache = false to try to solve problems
}
return new ViewEngineResult(new string[] { }); //we found nothing and we pretend we looked nowhere
}
 
public ViewEngineResult FindView(ControllerContext context, string viewName, string masterName, bool useCache)
{
if (IsTheRightDevice(context))
{
return BaseViewEngine.FindView(context, PathToSearch + "/" + viewName, masterName, false);//switching to useCache = false to try to solve problems
}
return new ViewEngineResult(new string[] { }); //we found nothing and we pretend we looked nowhere
}
 
public void ReleaseView(ControllerContext controllerContext, IView view)
{
throw new NotImplementedException();
}
}
 
public static class MobileHelpers
{
public static bool UserAgentContains(this ControllerContext c, string agentToFind)
{
return (c.HttpContext.Request.UserAgent.IndexOf(agentToFind, StringComparison.OrdinalIgnoreCase) > -1);
}
 
public static bool IsMobileDevice(this ControllerContext c)
{
return c.HttpContext.Request.Browser.IsMobileDevice;
}
 
public static bool IsTablet(this ControllerContext c)
{
return c.HttpContext.Request.Browser["is_tablet"].ToLower() == "true";
}
 
public static bool IsPhone(this ControllerContext c)
{
return IsMobileDevice(c) && c.HttpContext.Request.Browser["is_tablet"].ToLower() != "true";
}
 
public static void AddMobile<T>(this ViewEngineCollection ves, Func<ControllerContext, bool> isTheRightDevice, string pathToSearch)
where T : IViewEngine, new()
{
ves.Add(new CustomViewEngine(isTheRightDevice, pathToSearch, new T()));
}
 
public static void AddMobile<T>(this ViewEngineCollection ves, string userAgentSubstring, string pathToSearch)
where T : IViewEngine, new()
{
ves.Add(new CustomViewEngine(c => c.UserAgentContains(userAgentSubstring), pathToSearch, new T()));
}
 
public static void AddIPhone<T>(this ViewEngineCollection ves) //specific example helper
where T : IViewEngine, new()
{
ves.Add(new CustomViewEngine(c => c.UserAgentContains("iPhone"), "Mobile", new T()));
}
 
public static void AddIPad<T>(this ViewEngineCollection ves) //specific example helper
where T : IViewEngine, new()
{
//ves.Add(new CustomViewEngine(c => c.UserAgentContains("Firefox") || c.UserAgentContains("iPad"), "Mobile/iPad", new T()));
ves.Add(new CustomViewEngine(c => c.UserAgentContains("iPad"), "Tablet", new T()));
}
 
public static void AddAndroidTablet<T>(this ViewEngineCollection ves) //specific example helper
where T : IViewEngine, new()
{
//serve the ipad version to tablets with a screen larger than or equal to 8.9" otherwise it will fall through to the android phone one
//xoom is in wurfl but its physical screen size is not yet populated
ves.Add(new CustomViewEngine(c => c.UserAgentContains("android") && (c.UserAgentContains("xoom") || GetScreenSize(c.RequestContext.HttpContext.Request.Browser) >= 226), "Tablet", new T()));
}
 
public static void AddAndroidPhone<T>(this ViewEngineCollection ves) //specific example helper
where T : IViewEngine, new()
{
ves.Add(new CustomViewEngine(c => c.UserAgentContains("android") && GetScreenSize(c.RequestContext.HttpContext.Request.Browser) < 226, "Mobile/Droid", new T()));
}
 
public static void AddPhone<T>(this ViewEngineCollection ves)
where T : IViewEngine, new()
{
//matches mobile devices with a small screen size
ves.Add(new CustomViewEngine(c => c.IsPhone(), "Mobile", new T()));
}
 
public static void AddWP7<T>(this ViewEngineCollection ves)
where T : IViewEngine, new()
{
//matches mobile devices with a small screen size
ves.Add(new CustomViewEngine(c => c.UserAgentContains("Windows Phone OS 7") && c.UserAgentContains("IEMobile"), "Mobile/WP7", new T()));
}
 
public static void AddGenericMobile<T>(this ViewEngineCollection ves)
where T : IViewEngine, new()
{
ves.Add(new CustomViewEngine(c => c.IsMobileDevice(), "mobile", new T()));
}
 
public static void AddIE6<T>(this ViewEngineCollection ves) //specific example helper
where T : IViewEngine, new()
{
ves.Add(new CustomViewEngine(c => c.UserAgentContains("msie 6") || c.UserAgentContains("msie 5"), "ie", new T()));
}
 
public static void AddIELessThan9<T>(this ViewEngineCollection ves) //specific example helper
where T : IViewEngine, new()
{
ves.Add(new CustomViewEngine(c => c.HttpContext.Request.Browser.Browser == "IE" && c.HttpContext.Request.Browser.MajorVersion < 9, "ielt9", new T()));
}
 
public static void AddBlackBerryLessThan6<T>(this ViewEngineCollection ves) //specific example helper
where T : IViewEngine, new()
{
ves.Add(new CustomViewEngine(c => c.UserAgentContains("BlackBerry") && c.HttpContext.Request.Browser.MajorVersion < 6, "Mobile/BBlt6", new T()));
}
 
public static void AddIE<T>(this ViewEngineCollection ves) //specific example helper
where T : IViewEngine, new()
{
ves.Add(new CustomViewEngine(c => c.UserAgentContains("msie"), "ie", new T()));
}
 
private static double GetScreenSize(HttpBrowserCapabilitiesBase browser)
{
var screenWidth = double.Parse(browser["physical_screen_width"]);//in mm
var screenHeight = double.Parse(browser["physical_screen_height"]); //in mm
return Math.Sqrt(Math.Pow(screenWidth, 2) + Math.Pow(screenHeight, 2));
}
}
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.