I have an ASP.NET MVC web application, all the pages in which use a single master Layout.cshtml
page. Although I usually want to RenderBody()
, I have a site shutdown mechanism that can be enabled in my database so I basically want to have a layout page that looks something like:
@if(DbHelper.SiteIsShutDown) {
<h1>Site is shut down temporarily</h1>
}
else {
<h1>Welcome to the site</h1>
@RenderBody()
}
The trouble is that if SiteIsShutDown
is true, then RenderBody()
doesn't get called and I get the exception:
The "RenderBody" method has not been called for layout page...
So is there a way I can get round this? I just want to render some output from my layout page, and nothing from my view page.
You probably should leave the master layout to rendering views, and not short-circuit your views in the event of a site shutdown.
You're best bet is to check for this and handle it in Global.asax, i.e. in BeginRequest
:
protected void Application_BeginRequest(object sender, EventArgs e)
{
if(DbHelper.SiteIsShutDown)
{
HttpContext.Current.Response.Redirect("SiteDown");
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With