Skip to content

anonymous /customviewengine.cs
Created

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
CustomViewEngine
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));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.