How can I have an ASP Web Form (ASPX) with a Razor

2019-07-22 03:32发布

问题:

I have spent considerable time investigating having an ASP Web Form (ASPX) with a Razor MVC Layout, but have yet to make progress. I need this functionality since Webforms are faster to develop in, and I need to use the WebForm AjaxToolkit. I don't care about passing it a Controller or Model because I can handle all that in the code behind.

It seems to boil down to these options:

  1. ASP Master Page that builds a Razor Page

    I based this answer on http://weblogs.asp.net/imranbaloch/a-webform-view-with-a-razor-layout-in-asp-net-mvc-3 - unfortunatly I never got this to work. The problem I had was that in the Master ASP file, there's no link to a controller or model, so calling

    <%=Html.Partial('Page.cshtml')%>
    

    from the ASP Master page results in a null exception, because the read-only "Model" property is null, even if I do this in the ASPX codebehind that calls the master page:

    public partial class ViewPage : System.Web.Mvc.ViewPage<Model.Class.Here>
    

    As I mentioned above I don't care about the controller or model - is there a way to call HTML.Partial() with a dummy Model, just to get it to generate something?

  2. Render the layout to a string with RazorEngine

    I haven't been able to figure out how to get RazorEngine to load the existing MVC environment, so it can parse Html commands and expand Bundles etc. - If anyone has any guidance on how to use RazorEngine to just build the layout file from within an MVC project, it would be much appreciated.

  3. WebForm functionality within Razor (somehow)

    Alternatively if someone can tell me how I can use WebForm style controls which I can read directly and use the AjaxToolkit with inside Razor, I can forget this whole thing.


Option 1 Code Attempt:

WebForm Markup (Index.aspx):

<%@ Import Namespace="Bolee.Plugin.CheerRegistration.Models" %>
<%@ Page Title="" Language="C#" MasterPageFile="_Razor.Master" AutoEventWireup="true" CodeBehind="Index.aspx.cs"
Inherits="Views.Admin.ManageWebForm.ViewPage" %>
<!-- NOTE: Views.Admin.ManageWebForm.ViewPage<Models.Webform> here caused an error, so I moved this to the code behind --!>
<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">Title</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
Content
</asp:Content>

WebForm Code Behind (Index.aspx.cs):

namespace Views.Admin.ManageWebForm
{
    public partial class ViewPage : System.Web.Mvc.ViewPage<Models.WebForm>
    {

    }
}

Master Markup (_Razor.Master):

<%@ Master Language="C#" Inherits="System.Web.Mvc.ViewMasterPage" ViewStateMode="Disabled" EnableViewState="false" %>
<asp:PlaceHolder runat="server" Visible="false"><asp:ContentPlaceHolder ID="TitleContent" runat="server" /></asp:PlaceHolder>
<asp:PlaceHolder runat="server" Visible="false"><asp:ContentPlaceHolder ID="MainContent" runat="server"  /></asp:PlaceHolder>
<%@ Import Namespace="System.Web.Mvc" %>
<%@ Import Namespace="System.Web.Mvc.Html" %>
<%   
    var buildTitle = new StringBuilder();
    var buildMain = new StringBuilder();   
    TitleContent.RenderControl(new HtmlTextWriter(new System.IO.StringWriter(buildTitle)));
    MainContent.RenderControl(new HtmlTextWriter(new System.IO.StringWriter(buildMain)));

    ViewBag.Title = buildTitle.ToString();
    ViewBag.MainContent = buildMain.ToString();
%>
<%=Html.Partial('_Razor.cshtml', viewData: ViewData)%><!-- <- Null Exception error here from "Model" being null -->

CSHTML Markup (_Razor.cshtml):

@model Models.WebForm;

@{
    Layout = "~/Views/Shared/_AdminLayout.cshtml";
}

@Html.Raw(ViewBag.MainContent)