Jon*_*ell 7 c# asp.net-mvc razor
我已将MVC区域添加到现有的Web窗体项目中.我想在MVC项目的所有视图中使用母版页.我不明白我应该如何在MVC区域内引用WebForms的MasterPage.
我读过这两篇文章:
http://www.hanselman.com/blog/MixingRazorViewsAndWebFormsMasterPagesWithASPNETMVC3.aspx
http://www.eworldui.net/blog/post/2011/01/07/Using-Razor-Pages-with-WebForms-Master-Pages.aspx
我在controllers文件夹中添加了一个控制器扩展类.
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");
}
}
Run Code Online (Sandbox Code Playgroud)
在/Views/Shared
文件夹里面我添加了三个文件:
_SiteLayout.cshtml
@RenderBody()
Run Code Online (Sandbox Code Playgroud)
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>
Run Code Online (Sandbox Code Playgroud)
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>
Run Code Online (Sandbox Code Playgroud)
在我添加web.Config
的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>
Run Code Online (Sandbox Code Playgroud)
在我的控制器中,它正确返回MVC视图,但不显示MasterPage中的内容.如何引用Web窗体的母版页?
简而言之,你不能。Asp.net Webforms 控件需要视图状态并使用不同的“引擎”进行解析。仅仅因为它们都是 Microsoft 和 .Net 并不一定意味着它们是兼容的。但这并不意味着将它们放在一起运行是不可能的。
您始终可以使用引用母版页的标准 .aspx 页面,并且内部有一个 ajax 驱动的内容区域。该内容区域可以使用 MVC 控制器返回的 ActionResults 进行刷新。因此,虽然您看起来像是要访问 mysite.com/default.aspx,但内容实际上来自 mysite.com/Home/Index(或您保留默认“主页”的任何位置)。不过,它们仍然是完全独立的实体,并且任何一个实体对另一个实体的控件操作都可能导致 masterr 中的视图状态无效或 AntiForgeryToken 失败。
归档时间: |
|
查看次数: |
1864 次 |
最近记录: |