The test requires a home page entry point. Here, you can use the default route settings to test.
1. First, create an action in the default homecollector, as shown below:
Public ActionResult index ()
{
Return View ();
}
2. Create the MyLeyout. cshtml view under the view/home file. The content is as follows:
@{
// Some code
}
<! DOCTYPE html>
<Html>
<Head>
<Title> @ ViewBag. Title </title>
</Head>
<Body>
<Div>
Start rendering Body <br/>
@ RenderBody ()
Rendering Body ends <br/>
<Br/>
Start rendering ViewPage1 <br/>
@ RenderPage ("~ /Views/Home/ViewPage1.cshtml ")
End rendering ViewPage1 <br/>
<Br/>
Start rendering ViewPage2 <br/>
@ RenderPage ("~ /Views/Home/ViewPage2.cshtml ")
End rendering ViewPage2 <br/>
<Br/>
Learned Section usage <br/>
Start rendering Section <br/>
Declaration method 1 (recommended): SectionA: <br/>
@ * The reference page is neither marked nor abnormal. If the second parameter is true, the reference page must have the SectionA tag *@
@ RenderSection ("SectionA", false)
------- <Br/>
Declaration Method 2: SectionB: <br/>
@{
// Determine whether SectionB is marked
If (IsSectionDefined ("SectionB "))
{
@ RenderSection ("SectionB ")
}
}
------- <Br/>
Declaration method 3: SectionC: <br/>
@{
// When we test the undefined SectionC, an exception will occur on the page.
// @ RenderSection ("SectionC") // open the comment during the test
}
------- <Br/>
Rendering Sction ended <br/>
</Div>
</Body>
</Html>
3. Create an index. cshtm file in the view/home folder. The content is as follows:
@{
ViewBag. Title = "Homepage ";
Layout = "~ /Views/home/MyLeyout. cshtml ";
}
<! --
Render the entire page ~ The RenderBody () placeholder in/Views/EBusiness/MyLeyout. cshtml
-->
<Div>
Here is the rendering Body.
</Div>
<! --
~ The siction definition required by the RenderSection ("SectionA", false) placeholder in/Views/EBusiness/MyLeyout. cshtml
-->
@ Section SectionA {
<Div> here is SectionA: declared on the index page </div>
}
<! --
~ The siction definition required for the RenderSection ("SectionB") placeholder in/Views/EBusiness/MyLeyout. cshtml. Use IsSectionDefined ("SectionB") to determine
-->
@ Section SectionB {
<Div> here is SectionB: it is also declared on the index page </div>
}
<! --
~ The RenderSection ("SectionC") placeholder in/Views/EBusiness/MyLeyout. cshtml,
It is not defined here. If the code comment is opened, an exception is reported, so that it is not used to determine whether a placeholder exists.
-->
4. Create the ViewPage1.cshtml view as above
@{
// ViewBag. Title = "Homepage ";
Layout = null;
}
<! --
Render the entire page ~ The RenderPage () placeholder in/Views/EBusiness/MyLeyout. cshtml. The RenderPage placeholder can be used multiple times.
-->
<Div>
Here we will render all the content of viwpage1.chatml.
</Div>
5. Create the ViewPage2.cshtml view at the same position as above to prove that the RenderPage () placeholder can be declared multiple times.
@{
// ViewBag. Title = "Homepage ";
Layout = null;
}
<! --
Render the entire page ~ The RenderPage () placeholder in/Views/EBusiness/MyLeyout. cshtml. The RenderPage placeholder can be used multiple times.
-->
<Div>
This is how viwpage2.chatm2 is rendered.
</Div>
Looking at how much code is messy, you should be able to understand the comments.