我有一个使用 RenderAction 调用视图的 Masterpage (site.master)。目前视图返回“hello world”。
网站主:
<%@ Master Language="C#" Inherits="System.Web.Mvc.ViewMasterPage" %>
<html>
<head id="Head1" runat="server">
<title><asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server" /></title>
<asp:ContentPlaceHolder ID="ContentPlaceHolder2" runat="server" />
</head>
<body>
<% Html.RenderAction("Test", "Charts"); %>
<asp:ContentPlaceHolder ID="ContentPlaceHolder3" runat="server" >
<p>site.master</p>
</asp:ContentPlaceHolder>
<asp:ContentPlaceHolder ID="ContentPlaceHolder4" runat="server" />
</body>
</html>
测试.aspx:
<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<dynamic>" %>
hello world!
ChartsController.cs:
public ActionResult Test()
{
return View();
}
如果我更新视图以明确传递 Masterpage 的名称,我在调用 RenderAction 时会收到错误消息。
ChartsController.cs:
public ActionResult Test()
{
return View(null, "site");
}
错误:
内容控件必须是内容页或引用母版页的嵌套母版页中的顶级控件。
Stack Trace:
[HttpException (0x80004005): Content controls have to be top-level controls in a content page or a nested master page that references a master page.]
System.Web.UI.MasterPage.CreateMaster(TemplateControl owner, HttpContext context, VirtualPath masterPageFile, IDictionary contentTemplateCollection) +8857854
System.Web.UI.Page.get_Master() +54
System.Web.UI.Page.ApplyMasterPage() +15
System.Web.UI.Page.PerformPreInit() +45
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +328
如何设置我希望视图使用的母版页?最终,我将使用自定义 ViewEngine(通过覆盖 VirtualPathProviderViewEngine.FindView)动态设置 Masterpage。
if ( String.IsNullOrEmpty(masterName) ){ masterName = "site"; }
当我在 ViewEngine 中设置 masterName 属性,然后从 site.master 调用 RenderAction 时,我得到与在 Action 中设置 masterName 属性时相同的错误。
我正在使用:
Visual Studio 2010
MVC 3
IIS Express
编辑:添加了完整的 site.master 标记