I need to choose my spark master layout every time a request for a page is made. I attempted to so this by setting a ViewBag.Layout
value in OnActionExecuting
and referencing this value in the master layout ref.
<use master="${ViewBag.Layout}"/>
However, this doesn't work, it seems as if spark is not treating the code within the brackets not as code, but as a string. I get the following error:
The view 'Index' or its master was not found or no view engine supports the searched locations. The following locations were searched:
~/Views/Home/Index.aspx
~/Views/Home/Index.ascx
~/Views/Shared/Index.aspx
~/Views/Shared/Index.ascx
~/Views/Home/Index.cshtml
~/Views/Home/Index.vbhtml
~/Views/Shared/Index.cshtml
~/Views/Shared/Index.vbhtml
Layouts\${ViewBag.Layout}.spark
Shared\${ViewBag.Layout}.spark
Can anyone tell me why this is? Or point me to an alternative way of doing this?
The layout cannot be dynamically selected using code syntax. The reason for this is that that layout is selected prior to any rendering taking place in the engine. So first the layout is located, and then the engine tries to render all the variables in place. Using a variable for the layout means that the rendering engine doesn't know which file to open.