如何在ASP.NET MVC区域中的Web窗体中使用母版页 [英] How to use Master Page from Web Forms in ASP.NET MVC Area

查看:283
本文介绍了如何在ASP.NET MVC区域中的Web窗体中使用母版页的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已将MVC区域添加到现有的Web Forms项目中.我想在MVC项目的所有视图中使用母版页.我不明白如何在MVC区域内引用WebForms的MasterPage.

I have added an MVC Area to an existing Web Forms project. I want to use the Master Page in all of the views for the MVC project. I don't understand how I am supposed to reference the MasterPage for the WebForms inside the MVC Area.

我已经阅读了这两篇文章:

I have read these two articles:

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文件夹内添加了一个controllers扩展类.

I have added a controller extensions class inside the controllers folder.

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文件夹中,我添加了三个文件:

Inside the /Views/Shared folder I have added three files:

_SiteLayout.cshtml

_SiteLayout.cshtml

@RenderBody()

RazorView.aspx

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

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>

Views文件夹下Shared文件夹中的web.Config里面,我添加了:

Inside the web.Config in the Shared folder under Views I added:

<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窗体的母版页?

In my controller it returns the MVC view correctly but the content from the MasterPage does not appear. How do I reference the master page for the Web Forms?

推荐答案

简而言之,你做不到. Asp.net Webforms控件需要viewstate并使用不同的引擎"进行解析.仅仅因为它们都是Microsoft和.Net并不一定意味着它们是兼容的.但这并不意味着不可能将它们一起运行.

In short, you can't. Asp.net webforms controls requires viewstate and is parsed with a different "engine". Just because they're both Microsoft and .Net doesn't necessarily mean they are compatible. That doesn't mean running them together is impossible though.

您始终可以使用引用母版页的标准.aspx页,并且内部具有ajax驱动的内容区域.可以使用您的MVC控制器返回的ActionResults刷新此内容区域.因此,尽管看起来您将要访问mysite.com/default.aspx,但内容实际上将来自mysite.com/Home/Index(或保留默认"home"的任何位置).但是,它们仍然是完全独立的实体,任何一个控件对另一个控件的任何操纵都可能导致母版中的无效视图状态或AntiForgeryToken失败.

You could always use a standard .aspx page which references your master page, and inside have an ajax driven content area. This content area could be refreshed with the returned ActionResults of your MVC controllers. 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"). 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.

这篇关于如何在ASP.NET MVC区域中的Web窗体中使用母版页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆