Blocks without controller – how to structure views?

Vote:
 
Hi,

I would like to know how to structure views for blocks without controllers.
 
Beneath comes the details…

Documentation states “In Visual Studio, add a partial view with the same name as your block type and based on your block model class, to the Views/Shared folder of your project.” and “For performance reasons, it is recommended to use partial views directly, and not controllers, for block types.”.

So this implies there will be lot of views just placed in Views/Shared.

It can be used IViewTemplateModelRegistrator or PartialContentController approaches but this ones will eliminate the performance benifits, won’t they?
#185519
Edited, Nov 22, 2017 14:03
Vote:
 

I don't think you should be that worried about performance when using controllers for blocks. But if they're not needed, you should avoid them.

You can also create your own view engine and change how the partial views are discovered:

public class ViewEngine : RazorViewEngine
{
    private static readonly string[] AdditionalPartialViewFormats =
    {
        "~/Views/{0}/Partials/Index.cshtml",
        "~/Views/Shared/PagePartials/{0}.cshtml",
        "~/Views/Shared/Partials/{0}.cshtml",
        "~/Views/Shared/Blocks/{0}.cshtml",
    };

    public ViewEngine()
    {
        this.PartialViewLocationFormats = this.PartialViewLocationFormats.Union(AdditionalPartialViewFormats).ToArray();
    }
}

And in Application_Start in Global you can register your custom view engine:

ViewEngines.Engines.Add(new ViewEngine());
#185530
Nov 22, 2017 16:59
Vote:
 

Someone did actually measure the difference https://hacksbyme.net/2017/09/26/performance-when-using-controllers-for-blocks/

#185535
Nov 22, 2017 17:04
Vote:
 

Great, both post are useful.

#185544
Nov 23, 2017 9:39
Vote:
 

it's much more fun when you throw feature folders in the game...

#185653
Nov 28, 2017 23:36
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.