How to use homepage from web forms in ASP.NET MVC Area

I have added an MVC scope to an existing Web Forms project. I want to use the main page in all views for an MVC project. I do not understand how I should reference MasterPage for WebForms inside the MVC scope.

I 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

I added a class of controller extensions inside the controller 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");
    }
}

Inside the folder, /Views/SharedI added three files:

_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 web.Configin the folder Sharedin ViewsI 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 . -?

+4
1

, . - Asp.net "". , Microsoft .Net , . , .

.aspx, , - , ajax. ​​ ActionResults MVC. , , mysite.com/default.aspx, mysite.com/Home/Index ( "" ). , , masterr AntiForgeryToken.

+1

Source: https://habr.com/ru/post/1583784/


All Articles