Try our conversational search powered by Generative AI!

Block with and without controller


I want to create a block with two or three properties. 

I just want to know the difference between a block with and without controller. A block created without controller has the view (with block name) under shared folder, however for the one with controller we will have a separate folder with Block Name and Index view in it.

Could anyone please list out which one is preferable and why?

May 13, 2016 11:15

If you have simple blocks that just renders a few properties then avoid using a separate controller.

If you have plenty of logic that you need to take care of for the block, external data sources etc, then use a separate controller instead of making the poor views/html helpers have too much to worry about. 

So both are good ideas; Just depends on how advanced your block is...

Edited, May 13, 2016 11:24

If you switch from non-controller approach to controller approach -> sometimes you may wonder why your controller is not hit. Remember that EPiServer is using conventions to register templates for each content type. If there will be "reachable" template with the same name as your block type name -> your controller most probably will not be hit and EPiServer will skip that -> going directly to template.

Also - it's not necessary to have folder with block name and index.cshtml inside. What we are usually doing for blocks (it's subjective choice) - we do have templates "_{block-name}.cshtml" (_MyBlock.cshtml). This will make sure that EPiServer is not picking up template automatically.

May 14, 2016 22:54

From my memory, the block without controller has better performance. If your view only need to render that block data, personally, I would go with no controller approach. 

May 17, 2016 2:45

It really depends on complexity of the block logic. Sometimes there is no way to avoid controller for the block. But yes, you are true - invoking controller for the block costs more that dumb generation of the renderer template for the block.

May 17, 2016 8:45

Yup, better performance without controller and if you have a partial view you get perfect out of the box support for wysiwyg in edit mode as well. Otherwise you will need a little more tricks to get Ajax updates of blocks in edit mode to work...

May 17, 2016 8:45

Question: if you don't have a controller for the block, when you try to view the block (and not the page) in edit mode, you get a YSOD.

Any way to resolve this?

Edited, Feb 09, 2017 23:23

What says exception? I assume you have preview controller, right?

Feb 10, 2017 5:03

Yeah I have a preview controller - just cracked it, it was because the view for my preview controller was in the wrong place

Edited, Feb 10, 2017 12:29

Hi James,

Where was the view when it was in the wrong place, and where should it have been? I had trouble with preview before.


Feb 10, 2017 14:11
* 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.