Following the posts I've linked, I've made a naive implementation. Note: This is untested tested. For my testing I hardcoded IsBeta to return true, then it could not find any views (because I have no views named ViewName.beta.cshtml). After renaming Index.cshtml to Index.beta.cshtml it resolved the Index view as expected.
public class BetaViewEngineTest : RazorViewEngine
{
public BetaViewEngineTest() : base()
{
AreaViewLocationFormats = new string[] { "~/Areas/{2}/Views/{1}/{0}%1.cshtml",
"~/Areas/{2}/Views/{1}/{0}%1.vbhtml",
"~/Areas/{2}/Views/Shared/{0}%1.cshtml",
"~/Areas/{2}/Views/Shared/{0}%1.vbhtml" };
AreaMasterLocationFormats = new string[] { "~/Areas/{2}/Views/{1}/{0}%1.cshtml",
"~/Areas/{2}/Views/{1}/{0}%1.vbhtml",
"~/Areas/{2}/Views/Shared/{0}%1.cshtml",
"~/Areas/{2}/Views/Shared/{0}%1.vbhtml" };
AreaPartialViewLocationFormats = new string[] { "~/Areas/{2}/Views/{1}/{0}%1.cshtml",
"~/Areas/{2}/Views/{1}/{0}%1.vbhtml",
"~/Areas/{2}/Views/Shared/{0}%1.cshtml",
"~/Areas/{2}/Views/Shared/{0}%1.vbhtml" };
ViewLocationFormats = new string[] { "~/Views/{1}/{0}%1.cshtml",
"~/Views/{1}/{0}%1.vbhtml",
"~/Views/Shared/{0}%1.cshtml",
"~/Views/Shared/{0}%1.vbhtml" };
MasterLocationFormats = new string[] { "~/Views/{1}/{0}%1.cshtml",
"~/Views/{1}/{0}%1.vbhtml",
"~/Views/Shared/{0}%1.cshtml",
"~/Views/Shared/{0}%1.vbhtml" };
PartialViewLocationFormats = new string[] { "~/Views/{1}/{0}%1.cshtml",
"~/Views/{1}/{0}%1.vbhtml",
"~/Views/Shared/{0}%1.cshtml",
"~/Views/Shared/{0}%1.vbhtml" };
FileExtensions = new string[] { "cshtml", "vbhtml" };
}
protected override IView CreateView(ControllerContext controllerContext, string viewPath, string masterPath)
{
return base.CreateView(controllerContext, IsBeta(controllerContext) ? viewPath.Replace("%1", ".beta") : viewPath.Replace("%1", ""), masterPath);
}
protected override IView CreatePartialView(ControllerContext controllerContext, string partialPath)
{
return base.CreatePartialView(controllerContext, IsBeta(controllerContext) ? partialPath.Replace("%1", ".beta") : partialPath.Replace("%1", ""));
}
protected override bool FileExists(ControllerContext controllerContext, string virtualPath)
{
var isBeta = IsBeta(controllerContext);
if (isBeta)
{
return base.FileExists(controllerContext, virtualPath.Replace("%1", ".beta"));
}
else
{
return base.FileExists(controllerContext, virtualPath);
}
}
private bool IsBeta(ControllerContext ctx)
{
bool isBeta = false;
var httpCookie = ctx.HttpContext.Request.Cookies["Beta"];
if (httpCookie != null)
Boolean.TryParse(httpCookie.Value, out isBeta);
return isBeta;
}
}
Almost forgot, Global.asax.cs, add these lines:
ViewEngines.Engines.Clear();
ViewEngines.Engines.Add(new BetaViewEngineTest());