1

I have a default _siteLayout.cshtml file with a call to @RenderBody() in the centre of it, in between some markup and Razor code.

I have a complex .aspx page which has some markup that I wish to render where this call to @RenderBody() is made.

Essentially, I would like to make a call like this inside of a new file :

@{  
    Layout = "/Shared/_SiteLayout.cshtml";
    @RenderPage(Default.aspx);
}

However, it is not possible to make a call to a .aspx page in this case.

Is there any simple solution to this ?

Default.aspx :

    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>


    <link href="fullcalendar/fullcalendar.css" rel="stylesheet" type="text/css" />
    <link href="Styles/dark-hive/jquery.ui.all.css" rel="stylesheet" type="text/css"  >
      <link href="Styles/jquery-ui-1.7.3.custom.css" rel="stylesheet" type="text/css" />
        <link rel='stylesheet' type='text/css' href='fullcalendar/fullcalendar.print.css' media='print' />

    <script src="jquery/jquery-1.3.2.min.js" type="text/javascript"></script>
    <script src="jquery/jquery-ui-1.7.3.custom.min.js" type="text/javascript"></script>
    <script src="jquery/jquery.qtip-1.0.0-rc3.min.js" type="text/javascript"></script>
</head>
<body>
    <form id="form1" runat="server">
    <asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true">
    </asp:ScriptManager>
    <div id="updatedialog" style="font: 70% 'Trebuchet MS', sans-serif; margin: 50px;"
        title="Update or Delete Event">
        <table cellpadding="0" class="style1">
            <tr>
                <td class="alignRight">
                    Name:</td>
                <td class="alignLeft">
                    <input id="eventName" type="text" /><br /></td>
            </tr>
            <tr>
                <td class="alignRight">
                    Description:</td>
                <td class="alignLeft">
                    <textarea id="eventDesc" cols="30" rows="3" ></textarea></td>
            </tr>
            <tr>
                <td class="alignRight">
                    Start Time:</td>
                <td class="alignLeft">
                    <span id="eventStart"></span></td>
            </tr>
            <tr>
                <td class="alignRight">
                    End Time: </td>
                <td class="alignLeft">
                    <span id="eventEnd"></span><input type="hidden" id="eventId" /></td>
            </tr>
        </table>
    </div>
    <div id="addDialog" style="font: 70% 'Trebuchet MS', sans-serif; margin: 50px;" title="Add Event">
    <table cellpadding="0" class="style1">
            <tr>
                <td class="alignRight">
                    Name:</td>
                <td class="alignLeft">
                    <input id="addEventName" type="text" size="50" /><br /></td>
            </tr>
            <tr>
                <td class="alignRight">
                    Description:</td>
                <td class="alignLeft">
                    <textarea id="addEventDesc" cols="30" rows="3" ></textarea></td>
            </tr>
            <tr>
                <td class="alignRight">
                    Start Time:</td>
                <td class="alignLeft">
                    <span id="addEventStartDate" ></span></td>
            </tr>
            <tr>
                <td class="alignRight">
                    End Time:</td>
                <td class="alignLeft">
                    <span id="addEventEndDate" ></span></td>
            </tr>
        </table>

    </div>
    <div runat="server" id="jsonDiv" />
    <input type="hidden" id="hdClient" runat="server" />
    </form>
</body>
</html>
Simon Kiely
  • 5,880
  • 28
  • 94
  • 180

2 Answers2

0

You could externalize the sections that you want to reuse from this .aspx page into .ascx partials and then render them:

@{  
    Layout = "/Shared/_SiteLayout.cshtml";
}
<div>
    @Html.Partial("foo.ascx")
</div>
some other contents

And if you want to use a WebForms view with a Razor Layout, I am afraid that it's not possible. The contrary though is possible: using a Razor view with a WebForms masterpage.

Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • thank you for the answer. Is it possible in this case to make a call to @Html.Partial("Foo.aspx") and simply render the entire .aspx page that I wish to call ? That would be ideal. – Simon Kiely Mar 05 '12 at 18:12
  • No, it's not possible because I guess the aspx page overrides some content place holders which you don't have in a Razor layout. So simply externalize the section into a partial and reuse this partial. – Darin Dimitrov Mar 05 '12 at 18:14
  • Thank you for your response. I am a little confused by what you mean by "externalize the section into a partial and reuse the partial". I have the line <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %> at the top of my method, and the Default.aspx file contains majority HTML, while the Default.aspx.cs file contains entirely C#. Apologies for the beginner questions I am just having some trouble getting to grips with this. – Simon Kiely Mar 05 '12 at 18:21
  • @SimonKiely, ohhhh, no. In ASP.NET MVC views you don't have any code behind. Your page definition should look like this: `<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage" %>`. Notice how the view must derive from `System.Web.Mvc.ViewPage`. Are you trying to reuse some legacy WebForm? If, yes, you can simply forget about it. And what I meant by externalizing into an ASCX partial was to move the section of the view that you want to reuse into a separate partial but if you are saying that your ASPX page consists primary with C# code behind that's a lost cause. – Darin Dimitrov Mar 05 '12 at 18:27
  • Thank you again for the explanation. I don't have any code behind, I believe. I have tried renaming the file to .ascx and using @Html.Partial to render it, however this results in the error - CS1061: 'System.Web.WebPages.Html.HtmlHelper' does not contain a definition for 'Partial'. I will edit my question above to include the code I have thus far right now, can you see what might be going wrong? Thanks very much again for the help. – Simon Kiely Mar 05 '12 at 18:41
  • @SimonKiely, this aspx is not an ASP.NET MVC view. You cannot use it in an ASP.NET MVC application. ASP.NET MVC views must inherit from `System.Web.Mvc.ViewPage` as shown in my previous comment. And there is no .cs file associated to them. – Darin Dimitrov Mar 05 '12 at 18:48
  • So is there anything I can do in my case where I do have a .cs file associated ? – Simon Kiely Mar 05 '12 at 19:25
  • Accepted. I ended up using an iFrame with a call to the .aspx webpage contained within that, which provided the functionality I wished for. Your answers were invaluable in learning more, however, so thank you. – Simon Kiely Mar 05 '12 at 21:21
0

The simplest solution - render that page in an iFrame from within a partial view. Its an option but in general, bad practice in cases like this (see: Are iframes considered 'bad practice'?)

The right way to handle this is to break up that aspx into modules you can use - as Darin responded with.

Community
  • 1
  • 1
David Savage
  • 760
  • 5
  • 15