ASP.NET Web Pages - Page Layout
With Web Pages, creating a website with a consistent layout is easy.
Consistent Appearance
On the Internet, you will find many websites with a consistent look and style:
- Each page has the same header
- Each page has the same footer
- Each page has the same style and layout
With Web Pages, you can do this very efficiently. You can write reusable content blocks (such as the page header and footer) in a separate file.
You can also use a layout template (layout file) to define a consistent layout for all web pages of the site.
Content Blocks
Many websites have some content that is displayed on every page of the site (such as the page header and footer).
With Web Pages, you can use the @RenderPage() method to import content from different files.
Content blocks (from another file) can be imported anywhere in a web page. Content blocks can contain text, markup, and code, just like any ordinary web page.
Writing the common header and footer as separate files saves you a lot of work. You don't have to write the same content in every page; when the content changes, you only need to modify the header or footer file, and the corresponding content on every page in the site is updated.
The following shows how it appears in code:
Example
@RenderPage("header.cshtml")
Hello Web Pages
This is a paragraph
@RenderPage("footer.cshtml")
Run Example »
Layout Page
In the previous section, you saw that displaying the same content across multiple web pages is very easy.
Another way to create a consistent appearance is to use a layout page. A layout page contains the structure of a web page, not the content. When a web page (content page) links to a layout page, it is displayed according to the structure of the layout page (template).
The layout page uses the @RenderBody() method to embed the content page; otherwise, it is no different from a normal web page.
Each content page must begin with thelayout directive.
The following shows how it appears in code:
Layout page:
This is header text
@RenderBody()
© 2012 Runoob. All rights reserved.
Any web page:
@{Layout="Layout.cshtml";}
Welcome to Runoob.com
Lorem ipsum dolor sit amet, consectetur adipisicing elit,sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laborisnisi ut aliquip ex ea commodo consequat.
Run Example »
D.R.Y. - Don't Repeat Yourself
With the two ASP.NET tools, Content Blocks and Layout Pages, you can give your web application a consistent appearance.
These two tools save you a lot of work; you no longer need to repeat the same information on every page. Centralized markup, styles, and code make your web application easier to manage and maintain.
Preventing Files from Being Browsed
In ASP.NET, files whose names start with an underscore can be prevented from being browsed on the web.
If you don't want your content blocks or layout pages to be seen by your users, you can rename these files:
_header.cshtm
_footer.cshtml
_Layout.cshtml
Hiding Sensitive Information
In ASP.NET, the most common way to hide sensitive information (database passwords, email passwords, etc.) is to store it in a separate file named "_AppStart".
_AppStart.cshtml
@{
WebMail.SmtpServer = "mailserver.example.com";
WebMail.EnableSsl = true;
WebMail.UserName = "username@example.com";
WebMail.Password = "your-password";
WebMail.From = "your-name-here@example.com";
}