布局具有这样的:RenderSection()是否在ASP.NET Core的<environment>标签帮助器中工作?
<!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 { 这是开发人员的东西 } environment> –
user2818985
@ user2818985这很有道理。未定义的环境不会发出内部的东西,这会导致子视图失败。如果你添加,作为答案,我可以接受。 – grokky
有可能是一个更好的方式来实现你想要的 – user2818985