【发布时间】:2017-03-10 10:38:21
【问题描述】:
布局有这个:
<!DOCTYPE html>
<html>
<head>
<environment names="Development">@RenderSection("devCss", required: false)</environment>
<environment names="Staging,Production">@RenderSection("staproCss", required: false)</environment>
</head>
<body>
@RenderBody()
<environment names="Development">@RenderSection("devJs", required: false)</environment>
<environment names="Staging,Production">@RenderSection("staproJs", required: false)</environment>
</body>
</html>
查看有这个:
@section devCss { <link rel="stylesheet" href="foo.css" asp-append-version="true" /> }
@section staproCss { <link rel="stylesheet" href="foo.min.css" asp-append-version="true" /> }
@section devJs {}
@section staproJs {}
<h1>hello</h1>
当RenderSection() 在<environment> 标签之外时,一切正常。
在里面时,如上例所示,它会失败并出现无用的错误InvalidOperationException: The following sections have been defined but have not been rendered by the page at '_Layout.cshtml': 'staproCss, staproJs'. To ignore an unrendered section call IgnoreSection("sectionName").
这显然没有意义,因为所有部分都已定义。它抱怨的是一些,而不是其他的。
<environment> 标签助手是否允许 RenderSection() 在其中?
【问题讨论】:
-
只有那些与环境匹配的部分在运行时被定义。在将内容渲染到每个部分之前,您需要添加相应的环境检查。例如
@section devCss{ This is the dev stuff } -
@user2818985 这是有道理的。未定义的环境不会发出里面的东西,这会导致子视图失败。如果您将其添加为我可以接受的答案。
-
可能有更好的方法来实现你想要的
-
欢迎您;>
-
@user2818985 我根据您的评论添加了答案。
标签: c# asp.net razor asp.net-core tag-helpers