/**************************CustomWebViewPage.cs****************************************/
using System;
using System.Web.Mvc;
using System.Web.WebPages;
public abstract class CustomWebViewPage<T> : WebViewPage<T>
{
public HelperResult RenderSection(string name, Func<dynamic, HelperResult> defaultContents)
{
if (IsSectionDefined(name))
{
return RenderSection(name);
}
return defaultContents(null);
}
}
/***********************_Layout.cshtml****************************************/
<!DOCTYPE html>
<html>
<head><title>Sample View</title></head>
<body>
<h1>@ViewBag.Message</h1>
<p>
@RenderBody()
</p>
<footer>
@RenderSection("Footer", @<h2>Default Footer Content!</h2>)
</footer>
</body>
</html>
/*************************Views/web.config设置****************************************************/
/**********************************ExampleController .CS*****************************************************/
using System.Web.Mvc;
namespace Chapter16.Controllers {
public class ExampleController : Controller {
public ActionResult LayoutSample() {
ViewBag.Message = "Welcome to ASP.NET MVC!";
return View();
}
public ActionResult LayoutSampleMissingFooter() {
ViewBag.Message = "Welcome to ASP.NET MVC!";
return View();
}
/**************************LayoutSample.cshtml***********************************/
@{
Layout = "_Layout.cshtml";
}
<div>
An example of a view with a layout and footer defined
</div>
@section Footer {
Footer content defined in <strong>LayoutSample.cshtml</strong>.
}
/********************************LayoutSampleMissingFooter.cshtml****************************************/
@{
Layout = "_Layout.cshtml";
}
<div>
An example of a view with a layout and footer defined
</div>
}
}