Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Jonathan Ben Avraham 43 posts 216 karma points
    Jun 06, 2016 @ 12:33
    Jonathan Ben Avraham
    0

    Unable to reuse @RenderBody()

    I'm creating a template for blog posts and within the master template I have a part for @RenderBody() in the Blog Template, which has a child template called BlogPost. When I try to load the blog page which has the @RenderBody() in it, I get the following error:

    The file "~/Views/Blog.cshtml" cannot be requested directly because it calls the "RenderBody" method.
    

    However when I run the blog article from url it loads perfectly and renders the page.

    Why is this and how can I render my blog page without getting this error?

  • Filipe Sousa 43 posts 205 karma points
    Jun 06, 2016 @ 12:57
    Filipe Sousa
    100

    Hi Jonathan,

    I believe I are trying to render a page the uses the Master template as it's template. You cannot directly use templates in your pages that call @RenderBody.

    Try setting your blog page to use the child template that has the Master template as it's parent. You can use

    Layout = "MasterTemplate.cshtml";
    

    in your child template for that.

    Hope I could be of some help.

  • Jonathan Ben Avraham 43 posts 216 karma points
    Jun 07, 2016 @ 13:55
    Jonathan Ben Avraham
    0

    Hi Filipe Sousa,

    Thank you, it took me about 10-20 minutes after posting this to realise what I was doing wrong so now I have my master template structure like this:

    Blog (master using @RenderBody)

    Blog Overview (Blog Landing Page Template)

    Blog Post (Blog Single Post Page Template)

    Blog Comments (Blog Comments Section Template)

    Blog Comment (Blog Comment Form Template)

    Thanks for your help!

    Jonathan

Please Sign in or register to post replies

Write your reply to:

Draft