Skip to content

Instantly share code, notes, and snippets.

@benfoster
Created February 10, 2016 10:33
Show Gist options
  • Save benfoster/7cb62f7c6f91888a876f to your computer and use it in GitHub Desktop.
Save benfoster/7cb62f7c6f91888a876f to your computer and use it in GitHub Desktop.
Theme-able View Engine in ASP.NET MVC 4/5
private void InitializeViewEngine()
{
HostingEnvironment.RegisterVirtualPathProvider(new DynamicAssetVirtualPathProvider());
ViewEngines.Engines.Clear();
ViewEngines.Engines.Add(new ThemableViewEngine
{
Theme = ctx => DependencyResolver.Current.GetService<ISiteContext>().Theme.ThemeName
});
}
public class ThemableViewEngine : ThemeableRazorViewEngine
{
public ThemableViewEngine()
{
// ~/themes/default/views/projects/list.cshtml
ViewLocationFormats = new[] {
"~/Themes/{2}/Views/{1}/{0}.cshtml",
"~/Themes/{2}/Views/Shared/{0}.cshtml",
"~/Views/{1}/{0}.cshtml",
"~/Views/Shared/{0}.cshtml"
};
// Master pages must be inside a "shared" directory
// ~/themes/default/views/shared/_layout.cshtml
MasterLocationFormats = new[] {
"~/Themes/{2}/Views/Shared/{0}.cshtml",
"~/Views/Shared/{0}.cshtml"
};
// ~/themes/default/views/shared/_menu.cshtml
PartialViewLocationFormats = new[] {
"~/Themes/{2}/Views/{1}/{0}.cshtml",
"~/Themes/{2}/Views/Shared/{0}.cshtml",
"~/Views/{1}/{0}.cshtml",
"~/Views/Shared/{0}.cshtml"
};
ViewStartFileExtensions = new[] { "cshtml" };
}
}
@saYRam
Copy link

saYRam commented Mar 11, 2020

What is this? Do you have any article or implementation for this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment