In a .NET Razor Web Application i’m trying to programmatically set the Layout. I can not use _ViewStart.cshtml and don’t wont to set the @{ Layout = "..." } on every page. This is what I have come up with:
A base WebViewPage class:
public abstract class SitePage<T> : System.Web.Mvc.WebViewPage<T>
{
private object _layout;
public new dynamic Layout { get { return _layout; } }
public override void InitHelpers()
{
base.InitHelpers();
_layout = "~/Themes/" + Settings.Theme + "/Views/_Layout.cshtml";
}
}
And in the application web.config I specify all view to use this base page. But the Layout is never used it seems. What could be wrong here?
The
WebViewPageclass inherits fromWebPageBasethat has a property namedLayoutlike:You can override the
Layoutproperty, or change your _layout logic to achieve your purpose. For example:and/or in a view-page, you can set it too:
UPDATE: using a flag to avoid stack-over-flow in assigning _layout more that once: