Skip to content

Instantly share code, notes, and snippets.

@CrazyMORF
Last active August 29, 2015 14:07
Show Gist options
  • Save CrazyMORF/ce2ec6c7efb0f8966e59 to your computer and use it in GitHub Desktop.
Save CrazyMORF/ce2ec6c7efb0f8966e59 to your computer and use it in GitHub Desktop.
Razor View Engine that supports view "versioning" determined from RouteData
using System.Web.Mvc;
namespace VersionedViewEngineBench.Helpers
{
/* Add to Global.asax.cs
// Clears all previously registered view engines.
ViewEngines.Engines.Clear();
// Registers our Razor C# specific view engine.
ViewEngines.Engines.Add(new VersionedRazorViewEngine());
//Versioning
routes.MapRoute(
name: "Versioning",
url: "v{version}/{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional },
constraints: new { version = @"\d" });
*/
public class VersionedRazorViewEngine : RazorViewEngine
{
public string VersionKey { get { return "version"; } }
public VersionedRazorViewEngine()
{
AreaViewLocationFormats = new[]
{
"~/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[]
{
"~/Views/{1}/{0}%1%.cshtml",
//"~/Views/{1}/{0}%1%.vbhtml",
"~/Views/Shared/{0}%1%.cshtml",
//"~/Views/Shared/{0}%1%.vbhtml"
};
MasterLocationFormats = new[]
{
"~/Views/{1}/{0}%1%.cshtml",
//"~/Views/{1}/{0}%1%.vbhtml",
"~/Views/Shared/{0}%1%.cshtml",
//"~/Views/Shared/{0}%1%.vbhtml"
};
PartialViewLocationFormats = new[]
{
"~/Views/{1}/{0}%1%.cshtml",
//"~/Views/{1}/{0}%1%.vbhtml",
"~/Views/Shared/{0}%1%.cshtml",
//"~/Views/Shared/{0}%1%.vbhtml"
};
}
protected override IView CreatePartialView(ControllerContext controllerContext, string partialPath)
{
return base.CreatePartialView(controllerContext, GetViewPath(controllerContext, partialPath));
}
protected override IView CreateView(ControllerContext controllerContext, string viewPath, string masterPath)
{
return base.CreateView(
controllerContext, GetViewPath(controllerContext, viewPath), GetViewPath(controllerContext, masterPath));
}
protected override bool FileExists(ControllerContext controllerContext, string virtualPath)
{
return base.FileExists(controllerContext, GetViewPath(controllerContext, virtualPath));
}
private string GetViewPath(ControllerContext controllerContext, string virtualPath)
{
if (string.IsNullOrEmpty(virtualPath))
{
return string.Empty;
}
int version;
if (controllerContext.RouteData.Values.ContainsKey(VersionKey)
&& int.TryParse(controllerContext.RouteData.Values[VersionKey].ToString(), out version)
&& version > 1)
{
controllerContext.Controller.ViewData["version"] = version.ToString();
return base.FileExists(controllerContext, virtualPath.Replace("%1%", ".v" + version))
? virtualPath.Replace("%1%", ".v" + version)
: virtualPath.Replace("%1%", string.Empty);
}
else
{
controllerContext.Controller.ViewData["version"] = string.Empty;
return virtualPath.Replace("%1%", string.Empty);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment