新的ASP.NET核心框架使我们能够为不同的环境执行不同的html:
<environment names="Development"> <link rel="stylesheet" href="~/lib/material-design-lite/material.css" /> <link rel="stylesheet" href="~/css/site.css" /> </environment> <environment names="Staging,Production"> <link rel="stylesheet" href="https://ajax.aspnetcdn.com/ajax/bootstrap/3.0.0/css/bootstrap.min.css" asp-fallback-href="~/lib/material-design-lite/material.min.css" asp-fallback-test-class="hidden" asp-fallback-test-property="visibility" asp-fallback-test-value="hidden"/> <link rel="stylesheet" href="~/css/site.css" asp-append-version="true"/> </environment>
但是,如何在ASP.NET Core MVC Web应用程序的_Layout.cshtml中确定和可视化当前环境的名称?
例如,我想将环境名称(生产,分期,开发))可视化为HTML注释用于调试目的:
<!-- Environment name: @......... -->
解决方法
您可以通过执行注入您的视图中的服务IHostingEnvironment @inject Microsoft.AspNetCore.Hosting.IHostingEnvironment hostingEnv 并做一个@ hostingEnv.EnvironmentName