简体   繁体   English

如何在ASP.NET MVC区域中使用Web窗体中的母版页

[英]How to use Master Page from Web Forms in ASP.NET MVC Area

I have added an MVC Area to an existing Web Forms project. 我已将MVC区域添加到现有的Web窗体项目中。 I want to use the Master Page in all of the views for the MVC project. 我想在MVC项目的所有视图中使用母版页。 I don't understand how I am supposed to reference the MasterPage for the WebForms inside the MVC Area. 我不明白我应该如何在MVC区域内引用WebForms的MasterPage。

I have read these two articles: 我读过这两篇文章:

http://www.hanselman.com/blog/MixingRazorViewsAndWebFormsMasterPagesWithASPNETMVC3.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

I have added a controller extensions class inside the controllers folder. 我在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");
    }
}

Inside the /Views/Shared folder I have added three files: /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>

Inside the web.Config in the Shared folder under Views I added: 在我添加的Views下的Shared文件夹中的web.Config内:

<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>

In my controller it returns the MVC view correctly but the content from the MasterPage does not appear. 在我的控制器中,它正确返回MVC视图,但不显示MasterPage中的内容。 How do I reference the master page for the Web Forms? 如何引用Web窗体的母版页?

In short, you can't. 总之,你不能。 Asp.net webforms controls requires viewstate and is parsed with a different "engine". Asp.net webforms控件需要viewstate,并使用不同的“引擎”进行解析。 Just because they're both Microsoft and .Net doesn't necessarily mean they are compatible. 仅仅因为它们既是Microsoft又是.Net并不一定意味着它们是兼容的。 That doesn't mean running them together is impossible though. 但这并不意味着将它们放在一起是不可能的。

You could always use a standard .aspx page which references your master page, and inside have an ajax driven content area. 您始终可以使用引用主页的标准.aspx页面,并且内部有一个ajax驱动的内容区域。 This content area could be refreshed with the returned ActionResults of your MVC controllers. 可以使用MVC控制器的返回ActionResults刷新此内容区域。 So while it would look like you're going to mysite.com/default.aspx, the content would really be coming from mysite.com/Home/Index (or wherever you keep your default "home"). 因此,虽然看起来像是要访问mysite.com/default.aspx,但内容实际上来自mysite.com/Home/Index(或者您保留默认“home”的地方)。 They would still be completely separate entities though, and any manipulation of controls for one by the other could result in an invalid viewstate in the masterr or a failure of an AntiForgeryToken. 但是,它们仍然是完全独立的实体,并且对一个控件的任何控制操作都可能导致masterr中的viewstate无效或者AntiForgeryToken失败。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM