Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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");
    }
}

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

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?

like image 930
Jonathan Kittell Avatar asked Apr 21 '15 14:04

Jonathan Kittell


People also ask

How do I create a master page in ASP NET?

Let's add a new ASP.NET page to the project and bind it to the Site.master master page. Right-click on the project name in Solution Explorer and choose the Add New Item option. Select the Web Form template, enter the name About.aspx, and then check the "Select master page" checkbox as shown in Figure 7.

How does aspx work with MVC?

If you add a plain ASPX page to an ASP.NET MVC project, well, it just works like a charm without any changes to the configuration. If you invoke the ASPX page, the ASPX page is processed with viewstate and postbacks. If you invoke another URL mapped to a controller class, the related action method is run and results are served accordingly.

How to create a web form master page in WordPress?

After clicking on new item, Window will open, select Web Form->Web Forms Master Page (shown in the picture), After clicking the add button, master page 'site1.master' adds to our project. Step 3: Design the master page, using HTML.

What is the use of @Master in ASP NET?

The @Master directive is similar to the @Page directive that appears in ASP.NET pages. It defines the server-side language (C#) and information about the location and inheritance of the master page's code-behind class. The DOCTYPE and the page's declarative markup appears beneath the @Master directive.


1 Answers

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.

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.

like image 97
Joel Etherton Avatar answered Oct 26 '22 23:10

Joel Etherton