使用环境标签时NOTYF视图组件不起作用

发布于 2025-02-12 07:49:43 字数 1601 浏览 1 评论 0原文

在我的dotnet 5 MVC Web应用程序中,我正在使用 this toastnotification用于显示通知的库。长期以来它运行良好。正如文档所说,它需要_layout文件中的以下行

@await Component.InvokeAsync("Notyf")

,因此该文件的一部分看起来像:

<script src="~/js/site.js" asp-append-version="true"></script>
<script src="~/js/controls.js" asp-append-version="true"></script>

@await Component.InvokeAsync("Notyf")
@await RenderSectionAsync("Scripts", required: false)

现在我想缩小前两行中提到的js文件。因此,我为两个文件创建了缩小捆绑包,因此上述部分看起来像:

<environment names="Development">
    <script src="~/js/site.js" asp-append-version="true"></script>
    <script src="~/js/controls.js" asp-append-version="true"></script>
</environment>

<environment names="Staging,Production">
    <script src="~/js/_Bundles/common.min.js" asp-append-version="true"></script>
</environment>

@await Component.InvokeAsync("Notyf")
@await RenderSectionAsync("Scripts", required: false)

更改之后:

  1. 如果将环境设置为开发,则所有这些都像以前一样正常工作。
  2. 如果我将环境设置为生产,则notyf组件无法正确加载。显然,似乎甚至在可用之前就被使用了。 上收到以下错误

我在加载页面 .png“ alt =”在此处输入图像描述”>

href =“ https://i.sstatic.net/eax8k.png” rel =“ nofollow noreferrer”>

任何想法为什么会发生这种情况?我根本没有碰到这条线。任何解决方案或替代方法都将不胜感激。

In my dotnet 5 MVC web application, I'm using THIS ToastNotification library for showing notifications. It's working good since long. As documentation says, it requires following line in _Layout file

@await Component.InvokeAsync("Notyf")

So a part of this file looks something like:

<script src="~/js/site.js" asp-append-version="true"></script>
<script src="~/js/controls.js" asp-append-version="true"></script>

@await Component.InvokeAsync("Notyf")
@await RenderSectionAsync("Scripts", required: false)

Now I wanted to minify the js files mentioned in first two lines. So I created minified bundle for the two files, hence the above sections now looks like:

<environment names="Development">
    <script src="~/js/site.js" asp-append-version="true"></script>
    <script src="~/js/controls.js" asp-append-version="true"></script>
</environment>

<environment names="Staging,Production">
    <script src="~/js/_Bundles/common.min.js" asp-append-version="true"></script>
</environment>

@await Component.InvokeAsync("Notyf")
@await RenderSectionAsync("Scripts", required: false)

After this change:

  1. If I set environment to be Development, then all works correctly as before.
  2. If I set environment to be Production, the Notyf component doesn't load correctly. Apparantly it seems like it is being used even before it is available. I get the following error on loading page

enter image description here

enter image description here

Any Idea why is this happening? I did not touch this line at all. Any solution or alternate approach will be appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

意犹 2025-02-19 07:49:43

您需要在非“开发”环境中手动启用staticwebassetsfileprovider

// Program.cs
...
public class Program
{
  ...
  public static IHostBuilder CreateHostBuilder(string[] args) =>
    Host.CreateDefaultBuilder(args)
      .ConfigureWebHostDefaults(webBuilder =>
      {
        webBuilder.UseStartup<Startup>();

        // Add this "ConfigureAppConfiguration" method calling.
        webBuilder.ConfigureAppConfiguration((ctx, cb) =>
        {
          //    Please specify the condition that is true only when
          //    the application is running on your development environment.
          //    Notice that excludes the case that the environment is "Development".
          if (!ctx.HostingEnvironment.IsDevelopment() &&
               /* your prefer condition*/)
          {
            //    This call inserts "StaticWebAssetsFileProvider" into
            //    the static file middleware.
            StaticWebAssetsLoader.UseStaticWebAssets(
              ctx.HostingEnvironment, 
              ctx.Configuration);
          }
        });
        ...

You need to enable StaticWebAssetsFileProvider manually in a not "Development" environment:

// Program.cs
...
public class Program
{
  ...
  public static IHostBuilder CreateHostBuilder(string[] args) =>
    Host.CreateDefaultBuilder(args)
      .ConfigureWebHostDefaults(webBuilder =>
      {
        webBuilder.UseStartup<Startup>();

        // Add this "ConfigureAppConfiguration" method calling.
        webBuilder.ConfigureAppConfiguration((ctx, cb) =>
        {
          //    Please specify the condition that is true only when
          //    the application is running on your development environment.
          //    Notice that excludes the case that the environment is "Development".
          if (!ctx.HostingEnvironment.IsDevelopment() &&
               /* your prefer condition*/)
          {
            //    This call inserts "StaticWebAssetsFileProvider" into
            //    the static file middleware.
            StaticWebAssetsLoader.UseStaticWebAssets(
              ctx.HostingEnvironment, 
              ctx.Configuration);
          }
        });
        ...
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文