This section introduces the use of
ASP.NET WebPages to achieve a unified page layout.With Web Pages, creating a consistently laid out website is easy.
On the Internet, you'll find many websites that share a consistent look and feel:
Each page has the same header
Each page has the same bottom
Each page has the same style and layout
With Web Pages, you can do this very efficiently. You can write reused content blocks (such as page headers and footers) in a separate file.
You can also use layout templates (layout files) to define a consistent layout for all pages of your site.
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 the web page. Content blocks can contain text, markup, and code just like any normal web page.
Writing common headers and footers into separate files will save 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 bottom file, and you will see that the corresponding content of each page in the site has been updated.
Here's how it appears in code:
<html> <body> @RenderPage("header.cshtml") <h1>Hello Web Pages</h1> <p>This is a paragraph</p> @RenderPage("footer.cshtml") </body> </html>
In the previous section, you saw that it is very easy to display the same content on multiple web pages.
Another way to create a consistent look is to use layout pages. A layout page contains the structure of the web page, not the content. When a web page (content page) is linked to a layout page, it is displayed according to the structure of the layout page (template).
The @RenderBody() method is used to embed the content page in the layout page. Other than that, it is no different from a normal web page.
Every content page must start with a layout directive .
Here's how it appears in code:
<html> <body> <p>This is header text</p> @RenderBody() <p>© 2012 W3CSchool. All rights reserved.</p> </body> </html>
@{Layout="Layout.cshtml";} <h1>Welcome to w3cschool.cn</h1> <p> Lorem ipsum dolor sit amet, consectetur adipisicing elit,sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. ex ea commodo consequat. </p>
Through the two ASP.NET tools Content Blocks and Layout Pages, you can give your Web applications a consistent look and feel.
These two tools can save you a lot of work by not having to repeat the same information on every page. Centralized markup, styles, and code make your web applications easier to manage and maintain.
In ASP.NET, file names begin with an underscore to prevent these files from being viewed on the Internet.
If you don't want your content blocks or layout pages to be visible to your users, you can rename these files:
_header.cshtm
_footer.cshtml
_Layout.cshtml
In ASP.NET, the most common way to hide sensitive information (database passwords, email passwords, etc.) is to save this information in a separate file called "_AppStart".
@{ WebMail.SmtpServer = "mailserver.example.com"; WebMail.EnableSsl = true; WebMail.UserName = "[email protected]"; WebMail.Password = "your-password"; WebMail.From = "your-name [email protected]"; }
The above is about the layout content of ASP.NET WebPages. If you want a more convenient and unified web page layout, you can try ASP.NET Web Pages for layout.