Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there any way to get around the RenderBody() requirement?

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.

like image 602
Jez Avatar asked Oct 05 '22 23:10

Jez


1 Answers

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");
    }
}
like image 188
Jerad Rose Avatar answered Oct 10 '22 03:10

Jerad Rose