Latest web development tutorials

ASP.NET Web Pages Layout

By Web Pages, create a consistent layout of the site is a very easy thing to do.


Consistent appearance

On the Internet, you'll find it over the site have a consistent look and feel:

  • Each page has the same head
  • Each page has the same base
  • Each page has the same style and layout

By Web Pages, you are able to do so very efficiently. You can reuse blocks of content (such as the head and the bottom of the page) written in a separate file.

You can also use the layout template (layout files) to all pages of the site consistent with the definition of the layout.


Content Blocks (block content)

Many sites have some of the content is to be displayed (such as page header and footer) on every page in the site.

By Web Pages, you can use the@RenderPage () method to import content from different files.

Content block (from another file) anywhere on a Web page can be imported. Content block can contain text, markup and code, just like any ordinary web same.

It will be written in a separate file common header and footer, which will help you save a lot of work. You do not have to write the same content on each page, when the content changes, you simply change the head or bottom of the file, you can see the site of the content of each page have been updated.

The following code shows how it is presented:

Examples

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

Running instance »


Layout Page (page layout)

In the previous section, you see, and want to show the same content is very easy on multiple pages.

Another way to create a consistent look is to use a page layout. A layout page contains a structure on the page, rather than the content. When a Web page (content pages) link to page layout, it will be displayed according to the layout page (template) structure.

Page layout used @RenderBody () method to embed content pages, in addition, it does not make any difference with a normal web page.

Each content page must start with the layout instructions.

The following code shows how it is presented:

Page layout:

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

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>

Running instance »


DRY - Do not Repeat Yourself (do not repeat yourself)

By Content Blocks (block content) and Layout Pages (page layout) both ASP.NET tools, you can make your Web application displays a consistent look.

Both of these tools can help you save a lot of work, you do not have to repeat the same information on each page. Set tags, style and code for your Web applications easier to manage, easier to maintain.


Browse prevent files from being

In ASP.NET, the file names beginning with an underscore, you can prevent these files are browsing the Internet.

If you do not want your content block or page layout seen your users, you can rename these files:

_header.cshtm

_footer.cshtml

_Layout.cshtml


Hide sensitive information

In ASP.NET, hide sensitive information (database password, e-mail passwords, etc.) The most common method is to save this information in a file named "_AppStart" separate file.

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