Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ignore renderbody in razor _layout.cshtml

I have a page for what u need to be signed in to watch it. So I want to try this code:

<!DOCTYPE html>
<html lang="de">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <meta charset="utf-8" />
    <title>@ViewBag.Title - Meine ASP.NET MVC-Anwendung</title>
    <link href="~/favicon.ico" rel="shortcut icon" type="image/x-icon" />
    <meta name="viewport" content="width=device-width" />
    @Styles.Render("~/Content/css")
    @if (!Request.IsAuthenticated) {
        @Styles.Render("~/Content/signin")
    }
    @Scripts.Render("~/bundles/modernizr")
</head>
<body>
    @if (Request.IsAuthenticated) {
        <header class="navbar navbar-fixed-top" role="banner">
            <div class="container">
                <div class="navbar-header">
                    <button class="navbar-toggle" type="button" data-toggle="collapse" data-target=".bs-navbar-collapse">
                        <span class="icon-bar"></span>
                        <span class="icon-bar"></span>
                        <span class="icon-bar"></span>
                    </button>
                    <a class="navbar-brand" href="../">Home</a>
                </div>
                <nav class="collapse navbar-collapse bs-navbar-collapse" role="navigation">
                    <ul class="nav navbar-nav">
                        <li class="active">
                            <a href="#">Link 1</a>
                        </li>
                        <li>
                            <a href="#">Link 2</a>
                        </li>
                        <li>
                            <a href="#">Link 3</a>
                        </li>
                        <li>
                            <a href="#">Link 4</a>
                        </li>
                        <li>
                            <a href="#">Link 5</a>
                        </li>
                    </ul>
                </nav>
            </div>
        </header>
        <div id="body">
            @RenderBody()
        </div>
    } else {
        @RenderBody()
    }
    @Scripts.Render("~/bundles/jquery")
    @Scripts.Render("~/bundles/jqueryval")
    @Scripts.Render("~/bundles/bootstrap")
    <footer>
        <p>&copy; @DateTime.Now.Year - Meine ASP.NET MVC-Anwendung</p>
    </footer>
</body>
</html>

But when I start this I get this exception

The RenderBody-Method wasn't called for the layoutpage "~/Views/Shared/_Layout.cshtml"

How can I make razor ignore the RenderBody-method?

like image 380
Knerd Avatar asked Sep 02 '13 22:09

Knerd


People also ask

What is the use of renderbody in layout page?

As we all known, RenderBody is a placeholder where all the view-specific pages you create show up, "wrapped" in the layout page. RenderBody should be included on every layout page, regardless the nesting. The "RenderBody" method has not been called for layout page "~/Views/Shared/_Layout.cshtml".

How can I make razor ignore the renderbody-method?

How can I make razor ignore the RenderBody-method? Show activity on this post. Note that you actually can "ignore" the content if you really want to. Normally you write @RenderBody () in your view code, which evaluates the body content, sticks it in a HelperResult, and then writes that to the output stream.

How do I render content within a layout page?

The RenderBody method placement within the layout page determines where the content page will be rendered, but it is also possible to render other content supplied by the content page within a layout page. This is controlled by the placement of calls to the RenderSectionAsync method.

What is a razor layout page?

In a Razor layout page, renders the portion of a content page that is not within a named section. The HTML content to render. Learn how to use common layouts, share directives, and run common code before rendering views in an ASP.NET Core app. Part 9 of tutorial series on ASP.NET Core MVC.


1 Answers

Note that you actually can "ignore" the content if you really want to. Normally you write @RenderBody() in your view code, which evaluates the body content, sticks it in a HelperResult, and then writes that to the output stream. In doing so, MVC marks the body as having been rendered. You can trick it into thinking the body has been rendered without actually writing anything by writing @{ RenderBody(); } (notice the braces) or just RenderBody(); if already in a code context. This evaluates the body content without actually writing it to the output stream.

like image 177
daveaglick Avatar answered Oct 09 '22 16:10

daveaglick