November Happy Hour will be moved to Thursday December 5th.

Views and Controllers subfolder for blocks

Vote:
 

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

#204801
Edited, Jun 18, 2019 17:31
Vote:
 

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.

#204805
Jun 18, 2019 18:33
Vote:
 

Hey Paul,

Thanks a lot for the help! I will try utilizing what you've shown as well as what examples are provided in Alloy.

Update:

The method suggested in EpiAlloy worked perfectly! Can't thank you enough. 👍

#204806
Edited, Jun 18, 2019 19:51
This topic was created over six months ago and has been resolved. If you have a similar question, please create a new topic and refer to this one.
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.