Creating A Consistent Layout in ASP - Net Web Pages (Razor) Sites - The ASP
Creating A Consistent Layout in ASP - Net Web Pages (Razor) Sites - The ASP
CreatingaConsistentLayoutinASP.NETWebPages(Razor)Sites|TheASP.NETSite
ASP.NET
Home
Get Started
Learn
Hosting
Downloads
Community
Forums
Help
This article explains how you can use layout pages in an ASP.NET Web Pages Razor website to create
reusable blocks of content like headers and footers and to create a consistent look for all the pages in
the site.
What you'll learn:
How to create reusable blocks of content like headers and footers.
How to create a consistent look for all the pages in your site using a layout.
How to pass data at run time to a layout page.
These are the ASP.NET features introduced in the article:
Content blocks, which are files that contain HTMLformatted content to be inserted in multiple pages.
Layout pages, which are pages that contain HTMLformatted content that can be shared by pages on
the website.
The RenderPage, RenderBody, and RenderSection methods, which tell ASP.NET where to insert
page elements.
The PageData dictionary that lets you share data between content blocks and layout pages.
ASP.NET inserts the content blocks at the point where the RenderPage method is called in the main page. The
Let us know
whatpage
you is
think.
Yes
No
finished
merged
then sent
to the browser.
http://www.asp.net/webpages/overview/uilayoutsandthemes/3creatingaconsistentlook
1/17
10/01/2016
CreatingaConsistentLayoutinASP.NETWebPages(Razor)Sites|TheASP.NETSite
In this procedure, you'll create a page that references two content blocks a header and a footer that are located in
separate files. You can use these same content blocks in any page in your site. When you're done, you'll get a page
like this:
2/17
10/01/2016
CreatingaConsistentLayoutinASP.NETWebPages(Razor)Sites|TheASP.NETSite
<body>
<h1>IndexPageContent</h1>
<p>Thisisthecontentofthemainpage.</p>
</body>
</html>
Note It's common practice to store files that are shared among web pages in a folder named
Shared.
Notice that the file name is _Header.cshtml, with an underscore _ as a prefix. ASP.NET won't send a page to the
browser if its name starts with an underscore. This prevents people from requesting inadvertently or otherwise
these pages directly. It's a good idea to use an underscore to name pages that have content blocks in them,
because you don't really want users to be able to request these pages they exist strictly to be inserted into
other pages.
6. In the Shared folder, create a file named _Footer.cshtml and replace the content with the following:
<divclass="footer">©2012ContosoPharmaceuticals.Allrightsreserved.
</div>
7. In the Index.cshtml page, add two calls to the RenderPage method, as shown here:
<!DOCTYPEhtml>
<html>
<head>
<title>MainPage</title>
</head>
<body>
@RenderPage("~/Shared/_Header.cshtml")
<h1>IndexPageContent</h1>
http://www.asp.net/webpages/overview/uilayoutsandthemes/3creatingaconsistentlook
3/17
10/01/2016
CreatingaConsistentLayoutinASP.NETWebPages(Razor)Sites|TheASP.NETSite
<p>Thisisthecontentofthemainpage.</p>
@RenderPage("~/Shared/_Footer.cshtml")
</body>
</html>
This shows how to insert a content block into a web page. You call the RenderPage method and pass it the
name of the file whose contents you want to insert at that point. Here, you're inserting the contents of the
_Header.cshtml and _Footer.cshtml files into the Index.cshtml file.
8. Run the Index.cshtml page in a browser. In WebMatrix, in the Files workspace, rightclick the file and then select
Launch in browser.
9. In the browser, view the page source. For example, in Internet Explorer, rightclick the page and then click View
Source.
This lets you see the web page markup that's sent to the browser, which combines the index page markup with
the content blocks. The following example shows the page source that's rendered for Index.cshtml. The calls to
RenderPage that you inserted into Index.cshtml have been replaced with the actual contents of the header and
footer files.
<!DOCTYPEhtml>
<html>
<head>
<title>MainPage</title>
</head>
<body>
<divclass="header">
Thisisheadertext.
</div>
<h1>IndexPageContent</h1>
<p>Thisisthecontentofthemainpage.</p>
<divclass="footer">
©2012ContosoPharmaceuticals.Allrightsreserved.
</div>
</body>
</html>
4/17
10/01/2016
CreatingaConsistentLayoutinASP.NETWebPages(Razor)Sites|TheASP.NETSite
doesn't contain any actual content. After you've created a layout page, you can create web pages that contain the
content and then link them to the layout page. When these pages are displayed, they'll be formatted according to
the layout page. In this sense, a layout page acts as a kind of template for content that's defined in other pages.
The layout page is just like any HTML page, except that it contains a call to the RenderBody method. The position of
the RenderBody method in the layout page determines where the information from the content page will be
included.
The following diagram shows how content pages and layout pages are combined at run time to produce the finished
web page. The browser requests a content page. The content page has code in it that specifies the layout page to use
for the page's structure. In the layout page, the content is inserted at the point where the RenderBody method is
called. Content blocks can also be inserted into the layout page by calling the RenderPage method, the way you did
in the previous section. When the web page is complete, it's sent to the browser.
The following procedure shows how to create a layout page and link content pages to it.
1. In the Shared folder of your website, create a file named _Layout1.cshtml.
2. Replace any existing content with the following:
<!DOCTYPEhtml>
<html>
<head>
<title>StructuredContent</title>
<linkhref="~/Styles/Site.css"rel="stylesheet"type="text/css"/>
</head>
<body>
@RenderPage("~/Shared/_Header2.cshtml")
<divid="main">
@RenderBody()
</div>
<divid="footer">
©2012ContosoPharmaceuticals.Allrightsreserved.
http://www.asp.net/webpages/overview/uilayoutsandthemes/3creatingaconsistentlook
5/17
10/01/2016
CreatingaConsistentLayoutinASP.NETWebPages(Razor)Sites|TheASP.NETSite
</div>
</body>
</html>
You use the RenderPage method in a layout page to insert content blocks. A layout page can contain only one
call to the RenderBody method.
3. In the Shared folder, create a file named _Header2.cshtml and replace any existing content with the following:
<divid="header">CreatingaConsistentLook</div>
6/17
10/01/2016
CreatingaConsistentLayoutinASP.NETWebPages(Razor)Sites|TheASP.NETSite
These style definitions are here only to show how style sheets can be used with layout pages. If you want, you
can define your own styles for these elements.
6. In the root folder, create a file named Content1.cshtml and replace any existing content with the following:
@{
Layout="~/Shared/_Layout1.cshtml";
}
<h1>StructuredContent</h1>
<p>Loremipsumdolorsitamet,consecteturadipisicingelit,
seddoeiusmodtemporincididuntutlaboreetdoloremagnaaliqua.
Utenimadminimveniam,quisnostrudexercitationullamcolaboris
nisiutaliquipexeacommodoconsequat.Duisauteiruredolorin
reprehenderitinvoluptatevelitessecillumdoloreeufugiatnulla
pariatur.Excepteursintoccaecatcupidatatnonproident,suntin
culpaquiofficiadeseruntmollitanimidestlaborum.</p>
This is a page that will use a layout page. The code block at the top of the page indicates which layout page to
use to format this content.
7. Run Content1.cshtml in a browser. The rendered page uses the format and style sheet defined in _Layout1.cshtml
and the text content defined in Content1.cshtml.
http://www.asp.net/webpages/overview/uilayoutsandthemes/3creatingaconsistentlook
7/17
10/01/2016
CreatingaConsistentLayoutinASP.NETWebPages(Razor)Sites|TheASP.NETSite
You can repeat step 6 to create additional content pages that can then share the same layout page.
Note You can set up your site so that you can automatically use the same layout page for all
the content pages in a folder. For details, see Customizing SiteWide Behavior for ASP.NET
Web Pages http://go.microsoft.com/fwlink/?LinkId=202906 .
This procedure shows how to create a content page that has multiple content sections and how to render it using a
layout page that supports multiple content sections.
1. In the Shared folder, create a file named _Layout2.cshtml.
2. Replace any existing content with the following:
<!DOCTYPEhtml>
<html>
http://www.asp.net/webpages/overview/uilayoutsandthemes/3creatingaconsistentlook
8/17
10/01/2016
CreatingaConsistentLayoutinASP.NETWebPages(Razor)Sites|TheASP.NETSite
<head>
<title>MultisectionContent</title>
<linkhref="~/Styles/Site.css"rel="stylesheet"type="text/css"/>
</head>
<body>
<divid="header">
@RenderSection("header")
</div>
<divid="list">
@RenderSection("list")
</div>
<divid="main">
@RenderBody()
</div>
<divid="footer">
©2012ContosoPharmaceuticals.Allrightsreserved.
</div>
</body>
</html>
You use the RenderSection method to render both the header and list sections.
3. In the root folder, create a file named Content2.cshtml and replace any existing content with the following:
@{
Layout="~/Shared/_Layout2.cshtml";
}
@sectionheader{
<divid="header">
CreatingaConsistentLook
</div>
}
@sectionlist{
<ul>
<li>Lorem</li>
<li>Ipsum</li>
<li>Dolor</li>
<li>Consecte</li>
<li>Eiusmod</li>
<li>Tempor</li>
<li>Incididu</li>
</ul>
}
<h1>MultisectionContent</h1>
<p>Loremipsumdolorsitamet,consecteturadipisicingelit,
seddoeiusmodtemporincididuntutlaboreetdoloremagnaaliqua.
http://www.asp.net/webpages/overview/uilayoutsandthemes/3creatingaconsistentlook
9/17
10/01/2016
CreatingaConsistentLayoutinASP.NETWebPages(Razor)Sites|TheASP.NETSite
Utenimadminimveniam,quisnostrudexercitationullamcolaboris
nisiutaliquipexeacommodoconsequat.Duisauteiruredolorin
reprehenderitinvoluptatevelitessecillumdoloreeufugiatnulla
pariatur.Excepteursintoccaecatcupidatatnonproident,suntin
culpaquiofficiadeseruntmollitanimidestlaborum.</p>
This content page contains a code block at the top of the page. Each named section is contained in a section
block. The rest of the page contains the default unnamed content section.
4. Run Content2.cshtml in a browser.
10/17
10/01/2016
CreatingaConsistentLayoutinASP.NETWebPages(Razor)Sites|TheASP.NETSite
2. Save the page and then run it in a browser. An error message is displayed, because the content page doesn't
provide content for a section defined in the layout page, namely the header section.
3. In the Shared folder, open the _Layout2.cshtml page and replace this line:
@RenderSection("header")
As an alternative, you could replace the previous line of code with the following code block, which produces the
same results:
http://www.asp.net/webpages/overview/uilayoutsandthemes/3creatingaconsistentlook
11/17
10/01/2016
CreatingaConsistentLayoutinASP.NETWebPages(Razor)Sites|TheASP.NETSite
@if(IsSectionDefined("header")){
@RenderSection("header")
}
4. Run the Content2.cshtml page in a browser again. If you still have this page open in the browser, you can just
refresh it. This time the page is displayed with no error, even though it has no header.
The following procedure shows how to pass data from a content page to a layout page. When the page runs, it
displays a button that lets the user hide or show a list that's defined in the layout page. When users click the button,
it sets a true/false Boolean value in the PageData property. The layout page reads that value, and if it's false, hides
the list. The value is also used in the content page to determine whether to display the HideList button or the
ShowList button.
http://www.asp.net/webpages/overview/uilayoutsandthemes/3creatingaconsistentlook
12/17
10/01/2016
CreatingaConsistentLayoutinASP.NETWebPages(Razor)Sites|TheASP.NETSite
1. In the root folder, create a file named Content3.cshtml and replace any existing content with the following:
@{
Layout="~/Shared/_Layout3.cshtml";
PageData["Title"]="PassingData";
PageData["ShowList"]=true;
if(IsPost){
if(Request.Form["list"]=="off"){
PageData["ShowList"]=false;
}
}
}
@sectionheader{
<divid="header">
CreatingaConsistentLook
</div>
}
<h1>@PageData["Title"]</h1>
<p>Loremipsumdolorsitamet,consecteturadipisicingelit,
seddoeiusmodtemporincididuntutlaboreetdoloremagnaaliqua.
Utenimadminimveniam,quisnostrudexercitationullamcolaboris
nisiutaliquipexeacommodoconsequat.Duisauteiruredolorin
reprehenderitinvoluptatevelitessecillumdoloreeufugiatnulla
http://www.asp.net/webpages/overview/uilayoutsandthemes/3creatingaconsistentlook
13/17
10/01/2016
CreatingaConsistentLayoutinASP.NETWebPages(Razor)Sites|TheASP.NETSite
pariatur.Excepteursintoccaecatcupidatatnonproident,suntin
culpaquiofficiadeseruntmollitanimidestlaborum.</p>
@if(PageData["ShowList"]==true){
<formmethod="post"action="">
<inputtype="hidden"name="list"value="off"/>
<inputtype="submit"value="HideList"/>
</form>
}
else{
<formmethod="post"action="">
<inputtype="hidden"name="list"value="on"/>
<inputtype="submit"value="ShowList"/>
</form>
}
The code stores two pieces of data in the PageData property the title of the web page and true or false to
specify whether to display a list.
Notice that ASP.NET lets you put HTML markup into the page conditionally using a code block. For example, the
if/else block in the body of the page determines which form to display depending on whether
PageData["ShowList"]is set to true.
2. In the Shared folder, create a file named _Layout3.cshtml and replace any existing content with the following:
<!DOCTYPEhtml>
<html>
<head>
<title>@PageData["Title"]</title>
<linkhref="~/Styles/Site.css"rel="stylesheet"type="text/css"/>
</head>
<body>
<divid="header">
@RenderSection("header")
</div>
@if(PageData["ShowList"]==true){
<divid="list">
@RenderPage("~/Shared/_List.cshtml")
</div>
}
<divid="main">
@RenderBody()
</div>
<divid="footer">
<p>©2012ContosoPharmaceuticals.Allrightsreserved.</p>
</div>
</body>
</html>
http://www.asp.net/webpages/overview/uilayoutsandthemes/3creatingaconsistentlook
14/17
10/01/2016
CreatingaConsistentLayoutinASP.NETWebPages(Razor)Sites|TheASP.NETSite
The layout page includes an expression in the <title> element that gets the title value from the PageData
property. It also uses the ShowList value of the PageData property to determine whether to display the list
content block.
3. In the Shared folder, create a file named _List.cshtml and replace any existing content with the following:
<ul>
<li>Lorem</li>
<li>Ipsum</li>
<li>Dolor</li>
<li>Consecte</li>
<li>Eiusmod</li>
<li>Tempor</li>
<li>Incididu</li>
</ul>
4. Run the Content3.cshtml page in a browser. The page is displayed with the list visible on the left side of the
page and a HideList button at the bottom.
5. Click HideList. The list disappears and the button changes to ShowList.
http://www.asp.net/webpages/overview/uilayoutsandthemes/3creatingaconsistentlook
15/17
10/01/2016
CreatingaConsistentLayoutinASP.NETWebPages(Razor)Sites|TheASP.NETSite
Additional Resources
Customizing SiteWide Behavior for ASP.NET Web Pages http://go.microsoft.com/fwlink/?LinkId=202906
This article was originally created on March 10, 2014
Author Information
Tom FitzMackenTom FitzMacken is a Senior Programming Writer on the Web Platform & Tools
Content team.
Comments (16)
This site is managed for Microsoft by Neudesic, LLC. | 2016 Microsoft. All rights reserved.
http://www.asp.net/webpages/overview/uilayoutsandthemes/3creatingaconsistentlook
16/17
10/01/2016
CreatingaConsistentLayoutinASP.NETWebPages(Razor)Sites|TheASP.NETSite
http://www.asp.net/webpages/overview/uilayoutsandthemes/3creatingaconsistentlook
17/17