With Razor it is easy to create reusable HTML blocks like common headers and footers.
Importing Reusable Content Blocks
Many websites have content that is displayed on every page, like headers and footers.The @RenderPage() method lets you import blocks of content from a separate file that can contain text, markup, and code, just like a regular web page. The content block (from another file) can be inserted anywhere in your web pages.
Using this method saves you a lot of work. You don't have to write the same content into every page, and if you need to change the content, you can just update a single file, instead of updating all your pages.
When a browser requests a web page, the web server inserts content blocks where the @RenderPage() method is called in the page. The finished (merged) page is sent to the browser.
The following diagram shows how it works:
In the diagram above, two content blocks (a header and a footer) are located in separate files.
This is how it looks in code:
Example
<html><body>
@RenderPage("_header.cshtml")
<h1>Hello Razor</h1>
<p>This is a paragraph</p>
@RenderPage("_footer.cshtml")
</body>
</html>
No comments:
Post a Comment