Share the ASP. NET Learning Note (3) Webpages layout

Source: Internet
Author: User
with Web Pages, it's easy to create a layout-consistent site.

A consistent look

On the Internet, you'll find that websites have a consistent look and feel:

Each page has the same head

Each page has the same bottom

Each page has the same style and layout

With Web Pages, you can do this very effectively. You can write reusable chunks of content (such as the head of the page and the bottom) in a separate file.

You can also use a layout template (layout file) to define a consistent layout for all pages in your site.

Content Blocks (contents block)

Many sites have some content that is displayed on every page of the site (such as the head and bottom of the page).

Through the 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, tags, and code, just like any normal Web page.

Writing a single file with a common head and bottom will save you a lot of work. You do not have to write the same content on each page, and when the content changes, you can see that the corresponding contents of each page in the site have been updated as soon as you modify the header or the bottom file.

The following shows how it is rendered in the code:

Instance


Layout page

In the previous section, you saw that it was very easy to display the same content in multiple 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 page (content page) is linked 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, except that it does not differ from a normal web page.

Each content page must start with a layout instruction.

The following shows how it is rendered in the code:

Layout page:


Any Web page:

@{layout= "layout.cshtml";} 

D.r.y.-Don ' t Repeat yourself (don't repeat yourself)

With two ASP. Blocks (content blocks) and layout pages, you can have your WEB application display a consistent look.

These two tools can help you save a lot of work, and you don't have to repeat the same information on every page. Centralized tagging, styling, and code make your WEB applications easier to manage and easier to maintain.

Prevent files from being browsed

In ASP. NET, the name of the file begins with an underscore, which prevents these files from being browsed on the web.

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

Hide sensitive information

In ASP., the most common way to hide sensitive information (database passwords, e-mail passwords, and so on) 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";}

"Recommended"

1. ASP. NET free Video Tutorials

2. Share the ASP. NET Learning Notes (1)--webpages Razor

3. Share the ASP. NET Learning Notes (2)--webpages Introduction

4. ASP. NET's simple definition and introduction

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.