.net 尚未为布局页面“~/Views/Shared/index.cshtml”调用“RenderBody”方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/39308876/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
The "RenderBody" method has not been called for layout page "~/Views/Shared/index.cshtml"
提问by asif
The "RenderBody" method has not been called for layout page "~/Views/Shared/index.cshtml"
尚未为布局页面“~/Views/Shared/index.cshtml”调用“RenderBody”方法
An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
执行当前 Web 请求期间发生未处理的异常。请查看堆栈跟踪以获取有关错误及其在代码中的来源的更多信息。
how to fix this error
如何修复这个错误
回答by Tharusha
In my case after I add
在我添加后的情况下
<div class="container body-content">
@RenderBody()
<hr />
<footer><p>© @DateTime.Now.Year - My ASP.NET Application</p>
</footer>
</div>
to the shared layout's footer, all worked perfectly. this occurs because of the layout page on
到共享布局的页脚,一切都完美无缺。这是因为布局页面在
view -> shared
查看 -> 共享
haven't load the method @RenderBody()so you can either get rid of this using the upper code fragment or you can just comment the layout at the top of your relevant cshtml page.
还没有加载该方法,@RenderBody()因此您可以使用上面的代码片段来摆脱它,或者您可以在相关的 cshtml 页面顶部评论布局。
have fun coding cheers...
玩得开心编码欢呼...
回答by TareqBallan
please put this code just before "body" close tag at the end of your page
请将此代码放在页面末尾的“body”结束标记之前
@Scripts.Render("~/bundles/jquery")
@RenderSection("scripts", required: false)
回答by Chunde Huang
I got the same problem, my case is I use a nesting layout, you need to include @RenderBody for all _layout
我遇到了同样的问题,我的情况是我使用了嵌套布局,您需要为所有 _layout 包含 @RenderBody
read here for detail: https://forums.asp.net/t/2098873.aspx?The+RenderBody+method+has+not+been+called+for+layout+page+Views+Shared+_Layout+cshtml+
详情请阅读:https: //forums.asp.net/t/2098873.aspx?The+RenderBody+method+has+not+been+ called+for+layout+ page+Views+Shared+_Layout+cshtml+
回答by Rahul
I was getting same error when i was working on TagHelperComponent in my asp.net core MVC project. I googled a lot but everyone had a different problem than mine and obviously had a different solution than mine such as multiple layout files etc. Let me explain my problem -
当我在我的 asp.net 核心 MVC 项目中处理 TagHelperComponent 时,我遇到了同样的错误。我用谷歌搜索了很多,但每个人都遇到了与我不同的问题,并且显然有与我不同的解决方案,例如多个布局文件等。让我解释一下我的问题 -
i was making a CopyRight TagHelperComponent for injecting my copyright content (just before the body closing tag) into each and every view of my application like below -
我正在制作一个 CopyRight TagHelperComponent,用于将我的版权内容(就在正文结束标记之前)注入到我的应用程序的每个视图中,如下所示 -
public class CopyRightTagHelperComponent:TagHelperComponent
{
public CopyRightTagHelperComponent(string copyrightContent)
{
CopyrightContent = copyrightContent;
}
public string CopyrightContent { get; set; }
public override Task ProcessAsync(TagHelperContext context, TagHelperOutput output)
{
if (context.TagName.Equals("body", StringComparison.OrdinalIgnoreCase))
{
output.Content.AppendHtml(CopyrightContent);
}
return Task.CompletedTask;
}
}
but you can find the problem in my above code pretty well i believe. The problem is, i was trying to inject my copyright content in body tag (means not before the body closing tag, but with the Content property) by mistake. After spending a lot time, i observed that i used "Content" property to append my content instead of PreContent or PostContent property and that's what my mistake.
但是我相信你可以在我上面的代码中很好地找到问题。问题是,我试图错误地将我的版权内容注入 body 标签中(不是在 body 结束标签之前,而是使用 Content 属性)。花了很多时间后,我发现我使用“Content”属性来附加我的内容而不是 PreContent 或 PostContent 属性,这就是我的错误。
Cause of error was only, i was trying to append content with "Content" property. Changing my below line code from "Content" to "PostContent", error fixed -
错误的原因只是,我试图用“内容”属性附加内容。将下面的代码从“Content”更改为“PostContent”,错误已修复 -
if (context.TagName.Equals("body", StringComparison.OrdinalIgnoreCase))
{
output.PostContent.AppendHtml(CopyrightContent);
}
Important Node: By default, Tag Helper Components only operate on head & body elements.
重要节点:默认情况下,Tag Helper Components 仅对 head 和 body 元素进行操作。
To make them working for other elements like Footer, we need to create a new Tag Helper (named FooterTagHelper in case of footer element) which will derive from special class "TagHelperComponentTagHelper". For reference, see below code from Microsoft -
为了使它们适用于像页脚这样的其他元素,我们需要创建一个新的标签助手(在页脚元素的情况下命名为 FooterTagHelper),它将派生自特殊类“TagHelperComponentTagHelper”。作为参考,请参阅以下来自 Microsoft 的代码 -
Thanks
谢谢
回答by Ramio
Finally, I found the answer to this, I had the same problem and to fix it very EASY!, go to your view folder create _viewSrart.cshtml - then go to each view you have inside the views folder and delete or comment this ( Layout = "~/Views/Home/Index.cshtml";) it will work. thank me later
最后,我找到了这个问题的答案,我遇到了同样的问题并且很容易修复它!,转到您的视图文件夹创建 _viewSrart.cshtml - 然后转到您在视图文件夹中的每个视图并删除或评论此(布局= "~/Views/Home/Index.cshtml";) 它会起作用。晚点再谢我
回答by Mannan Bahelim
The “RenderBody” method has not been called for layout page
未为布局页面调用“RenderBody”方法
This typically occurs when you: – have a partial view – use a _ViewStart.cshtml page – you call the partival view from your controller using: return View();
这通常发生在您: – 有一个局部视图 – 使用 _ViewStart.cshtml 页面 – 您使用以下命令从控制器调用局部视图: return View();
And there you go wrong. It is a partial view, so you should return like this:
然后你就错了。这是一个局部视图,所以你应该像这样返回:
return PartialView();
返回 PartialView();
Source : http://www.cloud-developer.eu/blog/2014/01/20/renderbody-method-called-layout-page/
来源:http: //www.cloud-developer.eu/blog/2014/01/20/renderbody-method-called-layout-page/

