1

I have a page where I am validating a condition. if condition true display different page else displays the login page.

@if (sitemaintainmode == "false")
{
    <body>
   
        <div class="navbar navbar-default">
            <div class="container">
          
                    @Html.Partial("_LoginPartial")
               
            </div>
        </div>

        <div class="container body-content">
            @RenderBody()
        </div>
        @Html.Partial("_Footer")

      
        </script>
    </body>
}
else
{
   @RenderPage("~/Views/Account/Error.cshtml"); 
    
}

But it gives me an error -

The "RenderBody" method has not been called for layout page "~/Views/Shared/_Layout.cshtml".

D-Shih
  • 44,943
  • 6
  • 31
  • 51
Hob
  • 139
  • 1
  • 13
  • 1
    This would be much better handled in the controller than the layout page – Zeph Sep 20 '18 at 19:46
  • Yeap. In a `MVC` application usually such kind of decisions are taken before you get to the point where the actual view is rendered. I see that you check for maintenance. In this case maybe changing the routing would be a better solution and redirect all request to default action when the site is under maintenance. – Leron Sep 20 '18 at 19:48
  • routing from web.config or from controller? – Hob Sep 20 '18 at 19:55
  • @Hob check this question: https://stackoverflow.com/questions/7580911/implement-down-for-maintenance-page I think it's pretty close to what you want to achieve, but in a better way. – Leron Sep 20 '18 at 19:56
  • **Don't do this**. You are adding logic to view. A view is just to represent data. Your *logic* in this example should be done in the controller (or ActionFilter) and if it is determined to show the login page, then redirect there. – Erik Philips Sep 20 '18 at 20:43

2 Answers2

1

You can not ignore RenderBody() in your code because might be you use this Layout="~/Views/Shared/_Layout.cshtml" in your cshtml page.

Create seperate customeErrorPage (View) and Handle your condition in Controller and use return RedirectToAction("Login Or Error") or use return View("Login or Errorpage")

Hope this will help you.

  • Thanks!! As you suggested I added a condition in controller. it worked. public ActionResult Login(string returnUrl) { var sitemaintainmode = ConfigurationManager.AppSettings["IsSiteMaintenanceMode"]; if(sitemaintainmode == "true") { return View("Error"); } else { return View("Login"); } } – Hob Sep 21 '18 at 12:34
0

You could handle the action result in the controller, making a redirect to Error view in the Account controller or using customError in the web.config. For instance:

<customErrors mode="RemoteOnly" defaultRedirect="~/Home/Error_Generic/" >
  <error statusCode="500" redirect="~/Account/Error" />
</customErrors>
Ernesto Rodriguez
  • 257
  • 2
  • 9
  • 26