Virtual Classes Logo          Virtual Classes
    Read Anytime,Anywhere

Layout Page In MVC

Sometime we come across situations where we want to have common content that's displayed on every page, like header and footer. MVC lets you create a separate file with a content block that can contain text, markup, and code, just like a regular web page. You can then insert the content block in other pages on the site where you want the layout page content to appear. This way you don't have to copy and paste the same content into every page. If you need to change the content of your site, you just need update a single file, and the changes will be reflected everywhere the content has been inserted.

Layout Page folder structure-

Layout Page Folder Structure

The layout page is just like any page, the file name of layout page start with an underscore (_) as a prefix. MVC does not send a page request to the browser if page name starts with an underscore. This helps to prevent from requesting layout pages directly.
Layout page's content shared across the pages so it's common practice to store layout files in a Shared folder.
Shared folder keeps files which are common and used by multiple pages in the application.

Layout Page Source Code-

Layout Page Source Code

In the layout page, the content is inserted where the RenderBody method is called. A layout page can contain only one call to the RenderBody method.
You can use RenderPage method in the Layout page to render other page content in layout page. A layout page can have multiple RenderPage method.

Using Layout Page is web page-

Using Layout Page is web page