Using _ViewStart for areas for nested content

I have _ViewStart defining the main layout for my project (header, footer).

In this project, I have several areas. Each area has the same header and footer, as well as its own side menu. To do this, I created _ViewStart in the root directory of this area. Here is the (simplified) code:

/Views/_ViewStart.cshtml

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

/Views/Shared/_Layout.cshtml

  <html> <div> //header </div> <div> @RenderBody </div> </html> 

Foo scope -> /Areas/Foo/Views/_ViewStart.cshtml

 @{ Layout = "~/Views/Shared/_Layout.cshtml"; } <div class="row"> <div class="sidemenu"> //default sidemenu for this area </div> <div> @RenderBody() </div> </div> 

The page / Areas / Foo / Views / Bar / Index.cshtml will not be displayed, and I get this error:

CS0103: the name "RenderBody" does not exist in the current context

How to achieve this kind of nesting of the main page?

+6
source share
1 answer

I hate to answer my question, but here it is:

You cannot link to the root of the _ViewStart site directly to the _ViewStart of your area if you want a RenderBody there.

So the solution is:

/Views/_ViewStart.cshtml links /Views/Shared/_MainLayout.cshtml

/Areas/Foo/Views/_ViewStart.cshtml links /Areas/Foo/Views/Shared/_AreaLayout.cshtml

/Areas/Foo/Views/Shared/_AreaLayout.cshtml links /Views/Shared/_MainLayout.cshtml

What is it. You must use the General folder to have the RenderBody () method available.

+9
source

Source: https://habr.com/ru/post/908652/


All Articles