我已将 MVC 区域添加到现有的 Web 窗体项目中。我想在 MVC 项目的所有视图中使用母版页。我不明白我应该如何引用 MVC 区域内的 WebForms 的 MasterPage。
我读过这两篇文章:
http://www.hanselman.com/blog/MishingRazorViewsAndWebFormsMasterPagesWithASPNETMVC3.aspx http://www.hanselman.com/blog/MixingRazorViewsAndWebFormsMasterPagesWithASPNETMVC3.aspx
http://www.eworldui.net/blog/post/2011/01/07/Using-Razor-Pages-with-WebForms-Master-Pages.aspx http://www.eworldui.net/blog/post/2011/01/07/Using-Razor-Pages-with-WebForms-Master-Pages.aspx
我在控制器文件夹中添加了一个控制器扩展类。
public static class ControllerExtensions
{
public static ViewResult RazorView(this Controller controller)
{
return RazorView(controller, null, null);
}
public static ViewResult RazorView(this Controller controller, object model)
{
return RazorView(controller, null, model);
}
public static ViewResult RazorView(this Controller controller, string viewName)
{
return RazorView(controller, viewName, null);
}
public static ViewResult RazorView(this Controller controller, string viewName, object model)
{
if (model != null) controller.ViewData.Model = model;
controller.ViewBag._ViewName = GetViewName(controller, viewName);
return new ViewResult
{
ViewName = "RazorView",
ViewData = controller.ViewData,
TempData = controller.TempData
};
}
static string GetViewName(Controller controller, string viewName)
{
return !string.IsNullOrEmpty(viewName)
? viewName
: controller.RouteData.GetRequiredString("action");
}
}
在 - 的里面/Views/Shared
文件夹我添加了三个文件:
_SiteLayout.cshtml
@RenderBody()
RazorView.aspx
<%@ Page Title="" Language="C#" MasterPageFile="~/Areas/MyApp/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<dynamic>" %>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<% Html.RenderPartial((string) ViewBag._ViewName); %>
</asp:Content>
Site.Master
<%@ Master Language="C#" Inherits="System.Web.Mvc.ViewMasterPage" %>
<!DOCTYPE html>
<html>
<head runat="server">
<title></title>
<asp:ContentPlaceHolder ID="TitleContent" runat="server">
</asp:ContentPlaceHolder>
</head>
<body>
<div>
<asp:ContentPlaceHolder ID="MainContent" runat="server" />
</div>
</body>
</html>
在 - 的里面web.Config
in the Shared
下的文件夹Views
我补充道:
<system.web>
<pages pageParserFilterType="System.Web.Mvc.ViewTypeParserFilter, System.Web.Mvc, Version=5.2.2.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"
pageBaseType="System.Web.Mvc.ViewPage, System.Web.Mvc, Version=5.2.2.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"
userControlBaseType="System.Web.Mvc.ViewUserControl, System.Web.Mvc, Version=5.2.2.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
<controls>
<add assembly="System.Web.Mvc, Version=5.2.2.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" namespace="System.Web.Mvc" tagPrefix="mvc" />
</controls>
<namespaces>
<add namespace="System.Web.Mvc.Html"/>
</namespaces>
</pages>
</system.web>
在我的控制器中,它正确返回 MVC 视图,但 MasterPage 中的内容不会出现。如何引用 Web 表单的母版页?