asp.net-core.net-9.0

Why does ASP.NET Core 9 strip out importmap?


I have seen this on a newly-generated ASP.NET Core 9 app and in an app I upgraded to .NET 9 from .NET 8.

This is in Views/Shared/_Layout.cshtml:

   <script type="importmap">
      {
        "imports": {
            "axios": "/js/axios.min.js",
            "axios-retry": "/js/axios-retry.js",
            "is-retry-allowed": "/js/is-retry-allowed.js"
        }
      }
   </script>

and it gets stripped out. When I look at the devtools/sources tab in Chrome, it's there for the .NET 8 app, but not for the .NET 9 app.

How do I stop .NET 9 from doing that?

Edit: I found that the ScriptTagHelper class is the culprit.

If I add the following to _ViewImports.cshtml the importmap is not stripped out:

@removeTagHelper Microsoft.AspNetCore.Mvc.TagHelpers.ScriptTagHelper, Microsoft.AspNetCore.Mvc.TagHelpers

I'm guessing I don't want the ScriptTagHelper class permanently disabled (or do I?). Is there a way to tell it "leave my importmap alone"?


Solution

  • After checking the source code, you are missing asp-importmap when using importmap.

    Source code links:

    private const string ImportMapAttributeName = "asp-importmap";

    If we are missing asp-importmap attribute, we will get null exception in below code. That's why the script content not render in broswer.

    var importMap = ImportMap ?? ViewContext.HttpContext.GetEndpoint()?.Metadata.GetMetadata();

    Here is the soultion

    @using Microsoft.AspNetCore.Components
    @{
        var data = new ImportMapDefinition(
            new Dictionary<string, string>
            {
                { "axios", "/js/axios.min.js" },
                { "axios-retry", "/js/axios-retry.js" },
                { "is-retry-allowed", "/js/is-retry-allowed.js" },
            },null,null);
    }
    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="utf-8" />
        <meta name="viewport" content="width=device-width, initial-scale=1.0" />
        <title>@ViewData["Title"] - _79279047_1</title>
        <script type="importmap" asp-importmap="@data"></script>
    
        <link rel="stylesheet" href="~/lib/bootstrap/dist/css/bootstrap.min.css" />
        <link rel="stylesheet" href="~/css/site.css" asp-append-version="true" />
        <link rel="stylesheet" href="~/_79279047_1.styles.css" asp-append-version="true" />
    </head>
    <body>
        <header>
            <nav class="navbar navbar-expand-sm navbar-toggleable-sm navbar-light bg-white border-bottom box-shadow mb-3">
                <div class="container-fluid">
                    <a class="navbar-brand" asp-area="" asp-controller="Home" asp-action="Index">_79279047_1</a>
                    <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target=".navbar-collapse" aria-controls="navbarSupportedContent"
                            aria-expanded="false" aria-label="Toggle navigation">
                        <span class="navbar-toggler-icon"></span>
                    </button>
                    <div class="navbar-collapse collapse d-sm-inline-flex justify-content-between">
                        <ul class="navbar-nav flex-grow-1">
                            <li class="nav-item">
                                <a class="nav-link text-dark" asp-area="" asp-controller="Home" asp-action="Index">Home</a>
                            </li>
                            <li class="nav-item">
                                <a class="nav-link text-dark" asp-area="" asp-controller="Home" asp-action="Privacy">Privacy</a>
                            </li>
                        </ul>
                    </div>
                </div>
            </nav>
        </header>
        <div class="container">
            <main role="main" class="pb-3">
                @RenderBody()
            </main>
        </div>
    
        <footer class="border-top footer text-muted">
            <div class="container">
                &copy; 2024 - _79279047_1 - <a asp-area="" asp-controller="Home" asp-action="Privacy">Privacy</a>
            </div>
        </footer>
        <script src="~/lib/jquery/dist/jquery.min.js"></script>
        <script src="~/lib/bootstrap/dist/js/bootstrap.bundle.min.js"></script>
        <script src="~/js/site.js" asp-append-version="true"></script>
       
    
        @await RenderSectionAsync("Scripts", required: false)
    </body>
    </html>
    

    Test Result

    enter image description here