Error using @this.RenderBody
Problem reported by Osayed Zaheda - 5/12/2020 at 5:25 AM
Resolved
@page
@model DocumentViewer
@{
    ViewData["Title"] = "Document Viewer";
    Layout = "~/Pages/_Layout.cshtml";
}
@using GleamTech.AspNet.Core
@using GleamTech.DocumentUltimate
@using GleamTech.DocumentUltimate.AspNet
@using GleamTech.DocumentUltimate.AspNet.UI


Viewer

@{ var documentViewer = new DocumentViewer { Width = 800, Height = 600, Document = "~/Documents/Document.docx" }; } @this.RenderBody(documentViewer)

That is my code, It says this has no definition for RenderBody.

2 Replies

Reply to Thread
0
Cem Alacayir Replied
Employee Post
You are using a Razor page not Razor view:

The preceding code looks a lot like a Razor view file used in an ASP.NET Core app with controllers and views. What makes it different is the @page directive. @page makes the file into an MVC action - which means that it handles requests directly, without going through a controller. @page must be the first Razor directive on a page. @page affects the behavior of other Razor constructs. Razor Pages file names have a .cshtml suffix.
So it's a new type of page starting with .NET Core 3.0, reference:

The base class of the page is different so it can't find the extension method RenderBody and it says no definition.
Until we update the base class, you can use this workaround (simply cast this to RazorPageBase type):


@page
@using GleamTech.AspNet.Core
@using GleamTech.DocumentUltimate
@using GleamTech.DocumentUltimate.AspNet
@using GleamTech.DocumentUltimate.AspNet.UI
@using GleamTech.Examples
@using Microsoft.AspNetCore.Mvc.Razor

<!DOCTYPE html>
@{
    var documentViewer = new DocumentViewer
    {
        Width = 800,
        Height = 600,
        Document = "~/Documents/Document.docx"
    };
}
<html>
<head>
    <title>Document Viewer</title>
    @((RazorPageBase)this).RenderHead(documentViewer)
</head>
<body>
    @((RazorPageBase)this).RenderBody(documentViewer)
</body>
</html>
0
Cem Alacayir Replied
Employee Post Marked As Resolution
FYI, this is now fixed. You no longer need to use the above workaround with latest version:

Version 5.2.5 - May 22, 2020

  • Fixed: RenderHead and RenderBody were not available in ASP.NET Core 3.0's new Razor Pages (cshtml with @page directive).
    Changed base class for ASP.NET Core extension methods from RazorPage to RazorPageBase to fix it.


Reply to Thread