Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Razor Layout doesn't work if file is called _ViewStart.cshtml

I have an MVC3 web app to which I want to start using whole Razor views.

Most of the site is working fine with a Site.Master so I've packaged up most of it into partial views and am trying to add a layout with the same content.

After reading Scott Gu's blog I've added a file called _ViewStart.cshtml in my ~/Views directory so that it'll be applied by default.

Unfortunately this gives me the error:

The name 'RenderBody' does not exist in the current context

If the file is called _viewstart.cshtml.

If it is called _layoutviewstart.cshtml it will work but I have to reference it directly in the view.

If it's called _billyviewstart.cshtml is works but again I have to reference it directly in the view.

like image 799
Stu Avatar asked Aug 11 '11 11:08

Stu


2 Answers

View start

@{
    Layout = "~/Views/Shared/_Layout.cshtml";
}

Layout.cshtml

<!DOCTYPE html>
<html>
<body>
     @RenderBody()
</body>
</html>
like image 104
Nuri YILMAZ Avatar answered Nov 15 '22 22:11

Nuri YILMAZ


This error will be raised when you include @RenderBody directive on a page with @page directive at the top. By adding @page you have directed ASP.Net Core that it is a page and not layout. Therefore, the compiler does not expect RenderBody to be included on the page.

To fix this error, remove @page directive from layout.

like image 37
samets Avatar answered Nov 15 '22 22:11

samets