Share ASP.NET study notes (3) WebPages layout

零下一度
Release: 2017-05-24 17:57:41
Original
1991 people have browsed it

With Web Pages, creating a website with a consistent layout is easy.

Consistent look

On the Internet, you will find that many websites have a consistent look and feel:

Every page has the same header

Every page has the same bottom part

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 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.

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 is rendered in code:

Example

<html>
<body>
@RenderPage("header.cshtml")
<h1>Hello Web Pages</h1> 
<p>This is a paragraph</p>
@RenderPage("footer.cshtml")
</body>
</html>
Copy after login

Layout Page

In the previous section, you saw Now, it's very easy to display the same content in 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:

<html>
<body>
<p>This is header text</p>
@RenderBody()
<p>&copy; 2012 W3CSchool. All rights reserved.</p>
</body>
</html>
Copy after login

Any page:

@{Layout="Layout.cshtml";}<h1>Welcome to W3CSchool.cc</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.</p>
Copy after login

D.R.Y. - Don't Repeat Yourself (Don't Repeat Yourself)

With 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, 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 browsed

In 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 information

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".

_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";}
Copy after login

【Related recommendations】

1. ASP.NET free video tutorial

2. Share ASP.NET study notes (1 )--WebPages Razor

3. Share ASP.NET study notes (2)--WebPages introduction

4. A brief definition and introduction to ASP.NET

The above is the detailed content of Share ASP.NET study notes (3) WebPages layout. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!