Created
February 10, 2016 10:33
-
-
Save benfoster/7cb62f7c6f91888a876f to your computer and use it in GitHub Desktop.
Theme-able View Engine in ASP.NET MVC 4/5
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private void InitializeViewEngine() | |
{ | |
HostingEnvironment.RegisterVirtualPathProvider(new DynamicAssetVirtualPathProvider()); | |
ViewEngines.Engines.Clear(); | |
ViewEngines.Engines.Add(new ThemableViewEngine | |
{ | |
Theme = ctx => DependencyResolver.Current.GetService<ISiteContext>().Theme.ThemeName | |
}); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" }; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
What is this? Do you have any article or implementation for this?