为什么环境标记帮助程序在我的机器上有效,但在 Azure 上无效?

Why does the environment tag helper work on my machine but not on Azure?

这是关于我们 _Layout.cshtml 的部分,我将要放弃 window。

<!-- CSS Scripts -->
    <environment include="Development">
        <script src="/ts/CS.js" asp-append-version="true" abp-ignore-src-modification="true"></script>
        <script src="/ts/CS.Url.js" asp-append-version="true" abp-ignore-src-modification="true"></script>
        <script src="/ts/CS.Settings.js" asp-append-version="true" abp-ignore-src-modification="true"></script>
        <script src="/ts/CS.Alert.js" asp-append-version="true" abp-ignore-src-modification="true"></script>
        <script src="/ts/CS.Modal.js" asp-append-version="true" abp-ignore-src-modification="true"></script>
    </environment>
    <environment include="Staging,Production">
        <script abp-src="/ts/CS.js" asp-append-version="true"></script>
        <script abp-src="/ts/CS.Url.js" asp-append-version="true"></script>
        <script abp-src="/ts/CS.Settings.js" asp-append-version="true"></script>
        <script abp-src="/ts/CS.Alert.js" asp-append-version="true"></script>
        <script abp-src="/ts/CS.Modal.js" asp-append-version="true"></script>
    </environment>

我们在某些地方使用环境标签助手来使用 .min.js 或 .js,这对调试很有用。我可以看到,如果我亲自将 ASPNETCORE_ENVIRONMENT 变量从 "Development" 设置为 "Production",标签助手将继续呈现正确的内容。

但是,当我将其推送到 Azure 上时,内容完全丢失了。

我尝试过的事情:

这是怎么回事?!这不是别人做的吗?

暂存,生产服务器无法理解。您需要按如下方式进行更改。

如果您在本地工作,那么我们的脚本将在内部工作

<environment include="Development">
</environment>

如果您在像 azure 和其他服务器上工作,我们的脚本将在里面工作

<environment exclude="Development">
</environment>

您需要更改以下代码

<environment include="Staging,Production">
        <script abp-src="/ts/CS.js" asp-append-version="true"></script>
        <script abp-src="/ts/CS.Url.js" asp-append-version="true"></script>
        <script abp-src="/ts/CS.Settings.js" asp-append-version="true"></script>
        <script abp-src="/ts/CS.Alert.js" asp-append-version="true"></script>
        <script abp-src="/ts/CS.Modal.js" asp-append-version="true"></script>    </environment>

作为

<environment exclude="Development">
                <script abp-src="/ts/CS.js" asp-append-version="true"></script>
                <script abp-src="/ts/CS.Url.js" asp-append-version="true"></script>
                <script abp-src="/ts/CS.Settings.js" asp-append-version="true"></script>
                <script abp-src="/ts/CS.Alert.js" asp-append-version="true"></script>
                <script abp-src="/ts/CS.Modal.js" asp-append-version="true"></script> 
   </environment>

它可以在任何服务器上运行。

如果您想将相同的设置应用于 Development 以外的所有其他环境,则 exclude="Development" 有效。但是我遇到过需要为演出和制作设置不同的设置的时候。所以我必须要具体。

为了让环境标签助手在 Azure 上工作,我使用的解决方案是您需要在 设置 -> 配置 -> 应用程序设置[下为 ASPNETCORE_ENVIRONMENT 创建一个条目 在 Azure 上:

如果您没有在 Azure 上定义,当您发布时,如果我没记错的话,它将默认为 Production,这很奇怪。

我在使用 asp.net 3.0 时遇到了同样的问题。它在本地正确渲染环境,但当我部署到 Azure 时,它​​停止检测它并按原样渲染两个环境。

这是解决方案。只需在开头的 _Layout.cshtml 文件中添加以下行。

@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers

它会解决问题。

谢谢