2016-10-27 40 views
3

布局具有这样的: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()在其中?

+1

只有那些与环境相匹配的部分是在运行时定义的。在将内容呈现给每个部分之前,您需要添加相应的环境检查。例如, @section devCss { 这是开发人员的东西 } user2818985

+0

@ user2818985这很有道理。未定义的环境不会发出内部的东西,这会导致子视图失败。如果你添加,作为答案,我可以接受。 – grokky

+0

有可能是一个更好的方式来实现你想要的 – user2818985

回答

2

这个答案是这要感谢@ user2818985评论。

未定义的环境不会发出内容。这意味着它不会发出RenderSection()呼叫。这意味着该视图将定义一个不存在的section foo { ... }。哪个失败了,因此是例外。

为了实现我最初的目标,我更新了布局:

@inject Microsoft.AspNetCore.Hosting.IHostingEnvironment _env 
<!DOCTYPE html> 
<html> 
<head> 
    <environment names="Development"> 
     @RenderSection("devCss", required: false) 
    </environment> 
    <environment names="Staging,Production"> 
     @RenderSection("staproCss", required: false) 
    </environment> 
    @if (_env.EnvironmentName == "Development" && IsSectionDefined("staproCss")) 
    { 
     IgnoreSection("staproCss"); 
    } 
    @if (_env.EnvironmentName == "Staging,Production" && IsSectionDefined("devCss")) 
    { 
     IgnoreSection("devCss"); 
    } 
</head> 
<body> 
    @RenderBody() 
    <environment names="Development"> 
     @RenderSection("devJs", required: false) 
    </environment> 
    <environment names="Staging,Production"> 
     @RenderSection("staproJs", required: false) 
    </environment> 
    @if (_env.EnvironmentName == "Development" && IsSectionDefined("staproJs")) 
    { 
     IgnoreSection("staproJs"); 
    } 
    @if (_env.EnvironmentName == "Staging,Production" && IsSectionDefined("devJs")) 
    { 
     IgnoreSection("devJs"); 
    } 
</body> 
</html> 

所以部分总是定义的,因此孩子的意见不会抛出。

+0

我不认为'_env。EnvironmentName ==“Staging,Production”比较将起作用,因为环境或者是Staging或者Production ......不是两个。您需要一个函数来检查逗号分隔列表中是否存在真实环境名称('_env.EnvironmentName')。 –

+0

而不是把这些如果测试结束与_env变量,你可以把if放在每个环境块中,只是测试该部分是否定义。 –

1

不,环境标记用于根据ASPNET_ENV环境变量定义的环境呈现不同的HTML。例如,与生产环境相比,一组不同的CSS定义可以用于开发环境。

这个链接也可能会有所帮助: A Complete Guide to the MVC 6 Tag Helpers

您可以在自己的网站逻辑使用环境变量的值,如下所示。 enter image description here

请参阅此链接了解更多信息:Working with Multiple Environments

+0

我了解''标签帮手的工作原理及其用途。问题是它是否允许其中的RenderSection()?我期望如此,但我得到例外。 – grokky

+0

PS,你给的例子,正是我的问题所在。 :) – grokky

+0

我扩展了我的答案以解决您的问题的隐含目标。 – JohnH