Calling all developers! We invite you to provide your input on Feature Experimentation by completing this brief survey.
Calling all developers! We invite you to provide your input on Feature Experimentation by completing this brief survey.
Hi Gabe,
The Alloy demo site gives a good example of how to add to the locations searched for partial views by extending the RazorViewEngine:
namespace Alloy11.Business.Rendering
{
/// <summary>
/// Extends the Razor view engine to include the folders ~/Views/Shared/Blocks/ and ~/Views/Shared/PagePartials/
/// when looking for partial views.
/// </summary>
public class SiteViewEngine : RazorViewEngine
{
private static readonly string[] AdditionalPartialViewFormats = new[]
{
TemplateCoordinator.BlockFolder + "{0}.cshtml",
TemplateCoordinator.PagePartialsFolder + "{0}.cshtml"
};
public SiteViewEngine()
{
PartialViewLocationFormats = PartialViewLocationFormats.Union(AdditionalPartialViewFormats).ToArray();
}
}
}
Where the location constants mentioned are:
public const string BlockFolder = "~/Views/Shared/Blocks/";
public const string PagePartialsFolder = "~/Views/Shared/PagePartials/";
You then need to register the view engine in an IInitializableModule like this:
/// <summary>
/// Module for customizing templates and rendering.
/// </summary>
[ModuleDependency(typeof(EPiServer.Web.InitializationModule))]
public class CustomizedRenderingInitialization : IInitializableModule
{
public void Initialize(InitializationEngine context)
{
//Add custom view engine allowing partials to be placed in additional locations
//Note that we add it first in the list to optimize view resolving when using DisplayFor/PropertyFor
ViewEngines.Engines.Insert(0, new SiteViewEngine());
}
public void Uninitialize(InitializationEngine context)
{
}
public void Preload(string[] parameters)
{
}
}
For controllers, yes, you should be able to put them in a sub-folder without issue as they are not looked up by location in the same way as views.
Hello,
If I want to store all of my blocks in a subfolder within Views folder called Blocks(~/Views/Blocks/...) as opposed to having all of my blocks in Views with all of the standard Views(~/Views/...) what is the best way of going about doing that?
Also, is this possible with the Controller files as well(~/Controllers/Blocks/...)?
Thanks,
Gabe