.Net Core 3.1 Hosting Environment in _Layout.cshtml Error: CS0841: Cannot use local variable 'hostingEnv' before it is declared

.Net Core 3.1 Hosting Environment in _Layout.cshtml Error: CS0841: Cannot use local variable 'hostingEnv' before it is declared

我收到错误:CS0841 在声明之前无法使用局部变量 'hostingEnv'。

在我的顶部 _Layout.cshtml:

@inject Microsoft.AspNetCore.Hosting.IWebHostEnvironment hostingEnv

然后我得到错误:

        @if (hostingEnv.EnvironmentName == "Development")
        {
            @:<span>In Development</span>
        }

谢谢!

让我们尝试另一种访问环境的方法:

<environment include="Development">
  <div>The effective tag is: &lt;environment include="Development"&gt;</div>
</environment>
<environment exclude="Development">
  <div>The effective tag is: &lt;environment exclude="Development"&gt;</div>
</environment>
<environment include="Staging,Development,Staging_2">
  <div>
    The effective tag is:
    &lt;environment include="Staging,Development,Staging_2"&gt;
  </div>
</environment>

https://docs.microsoft.com/en-us/aspnet/core/fundamentals/environments?view=aspnetcore-5.0