Web Pages layout
ASP.NET Web Pages - Page Layout
Creating a website with a consistent layout is easy with Web Pages.
CONSISTENT LOOK
On the Internet, you will find that many websites have a consistent look and feel:
Everyone Pages have the same header
Every page has the same bottom
Every 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.
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 import content from different files using the @RenderPage() method.
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.
Write the common header and bottom into separate files, which 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.
The following shows how it appears in code:
Example
<html> <body> @RenderPage("header.cshtml") <h1>Hello Web Pages</h1> <p>This is a paragraph</p> @RenderPage("footer.cshtml") </body> </html>
Running Example»
Click the "Run Instance" button to view the online instance
Layout Page
In the previous part, you saw, what more do you want to do It's 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 in the layout page to embed the content 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 renders in code:
Layout page:
<body>
<p>This is header text</p>
@RenderBody()
<p>© 2012 W3CSchool. All rights reserved.</p>
< ;/body>
</html>
Any web page:
Example
@{Layout="Layout.cshtml";} <h1> Welcome to W3Cschool </h1> <p>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. Duis aute irure dolor inreprehenderit in voluptate velit esse cillum dolore eu fugiat nullapariatur. Excepteur sint occaecat cupidatat non proident, sunt inculpa qui officia deserunt mollit anim id est laborum.</p>
Run Instance»
Click the "Run Instance" button to view the online instance
# #D.R.Y. - Don't Repeat YourselfWith the two ASP.NET tools Content Blocks and Layout Pages, you can make your web application display Consistent appearance. These two tools can save you a lot of work, you don't have to repeat the same information on every page. Centralized markup, styles, and code make your web applications easier to manage and maintain.
Prevent files from being browsedIn ASP.NET, file names starting with an underscore can prevent these files from being browsed on the Internet. If you do not want your content blocks or layout pages to be visible to your users, you can rename these files: _header.cshtm_footer.cshtml_Layout.cshtml
Hide sensitive informationIn ASP.NET, the most common way to hide sensitive information (database passwords, email passwords, etc.) is to This information is saved in a separate file named "_AppStart".
WebMail.EnableSsl = true;
WebMail.UserName = "username@example.com";
WebMail.Password = "your-password";
WebMail.From = "your-name-here@example.com";
}