Skip to content

Razor regression in ASP .NET Core 9.0 (script type=importmap and others) #58973

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
1 task done
AlexBAV opened this issue Nov 15, 2024 · 11 comments
Open
1 task done

Razor regression in ASP .NET Core 9.0 (script type=importmap and others) #58973

AlexBAV opened this issue Nov 15, 2024 · 11 comments
Labels
area-mvc Includes: MVC, Actions and Controllers, Localization, CORS, most templates

Comments

@AlexBAV
Copy link

AlexBAV commented Nov 15, 2024

Is there an existing issue for this?

  • I have searched the existing issues

Describe the bug

The following razor snippet renders to nothing (is completely ignored??) in ASP.NET Core 9.0. Used to work correctly in previous version:

<script type="importmap">
	 {
		 "imports": {
		 }
	}
</script>

Looks like a bug in server-side script tag helper.

IMPORTANT! Suggested Workaround:

See below for a suggested workaround.

Expected Behavior

Should render "as is", without modifications

Steps To Reproduce

No response

Exceptions (if any)

No response

.NET Version

9.0.100

Anything else?

No response

@ghost ghost added the area-mvc Includes: MVC, Actions and Controllers, Localization, CORS, most templates label Nov 15, 2024
@ahazelwood
Copy link

I would like to upgrade to 9.0, however this is hitting me on a asp.net core mvc project upgrading from net8.0. Any suggestions for how to get it to render correctly would be greatly appreciated. I concur that it should not override anything but render it as written.

@AlexBAV
Copy link
Author

AlexBAV commented Nov 21, 2024

I currently use the following workaround for tag helpers that definitely should not be processed on a server side:

<!script type="importmap">...</!script>

<!tag> syntax is an official way to turn off tag helper processing.

However, I'm still sure that tag helper should not remove a tag if it cannot process it on a server side.

@ahazelwood
Copy link

ahazelwood commented Nov 21, 2024 via email

@AlexBAV
Copy link
Author

AlexBAV commented Nov 21, 2024

BTW, I suggest using this workaround for all "non-default" usages of script tag. It looks like they correctly recognize <script type="text/javascript">, <script type="module"> and <script>. All other usages should be guarded.

We, for example also had to implement a workaround for <script type="application/ld+json"> as it was also removed after upgrade to ASP.NET Core 9.0

@StevePy
Copy link

StevePy commented Nov 22, 2024

This one caught me as well, a bit sweaty when my importmap (part of _Layout.cshtml) wasn't rendered, considering the relatively good feeling I had combining razor views with ES6 modules. Fortunately the work-around with the <!script type="importmap"> works a treat, but I can't see how that should be an expected breaking change since it's kind of a necessity for module scripts which are recognized. Fortunately a small work-around and pretty much everything else with the switch to .Net 9 has been relatively painless.

@seangwright
Copy link
Contributor

I just spent a lot of time trying to figure out why my perfectly valid <script type="importmap"> element wasn't working and my scripts weren't loading.

It turned out the import map wasn't showing up in my HTML response from ASPNET Core...

When I add the ! to the <script> element it renders but now VS Code has lost any concept of Razor formatting this file with the C# dev kit. 🤦🤦

@seangwright
Copy link
Contributor

seangwright commented Feb 20, 2025

Here's the cause of the issue
https://github.com/dotnet/aspnetcore/blob/v9.0.2/src/Mvc/Mvc.TagHelpers/src/ScriptTagHelper.cs#L251

public override void Process(TagHelperContext context, TagHelperOutput output)
{
    ArgumentNullException.ThrowIfNull(context);
    ArgumentNullException.ThrowIfNull(output);

    if (string.Equals(Type, "importmap", StringComparison.OrdinalIgnoreCase))
    {
        // This is an importmap script, we'll write out the import map and
        // stop processing.
        var importMap = ImportMap ?? ViewContext.HttpContext.GetEndpoint()?.Metadata.GetMetadata<ImportMapDefinition>();
        if (importMap == null)
        {
            // No importmap found, nothing to do.
            output.SuppressOutput();
            return;
        }

        // ...
    }

    // ...
}

Because the ScriptTagHelper targets every <script> with a type attribute it will always execute for a <script type="importmap"> element.

Additionally, if you aren't using the import map feature that's part of Razor components then ASP.NET Core assumes the <script type="importmap"> is invalid and suppresses the output!

Hey, guess what ASP.NET Core... you don't get to decide if valid HTML is rendered or not 😠.

It looks like the regression was added in this PR #56045

@marchy
Copy link

marchy commented Feb 23, 2025

Nice chase-down @seangwright.

This is definitely a big with the .NET 9 framework. Team please resolve this. We are NOT using the new static asset pipeline and the behaviour of the rendering of our website should not change by upgrading – this is an unintentional regression.

@donnyv
Copy link

donnyv commented Mar 7, 2025

The worst part about this bug is that this post doesn't come up as a solution when you Google it. A stackoverflow post comes up about using the new asp-importmap="@data". As @seangwright correctly said. Asp.net should not be changing how html elements are rendered or should be rendered. Unless a "asp-*" attribute is added. Can't believe this got by testing. Spent way to long on this issue.

@seangwright
Copy link
Contributor

@AlexBAV could you change the title of this issue to mention "importmap"? It might make it easier for others to find.

@AlexBAV AlexBAV changed the title Razor regression in ASP .NET Core 9.0 Razor regression in ASP .NET Core 9.0 (script type=importmap and others) Mar 8, 2025
@AlexBAV
Copy link
Author

AlexBAV commented Mar 8, 2025

@AlexBAV could you change the title of this issue to mention "importmap"? It might make it easier for others to find.

Is it better now?

I've also modified a top-level post with a link to the workaround.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-mvc Includes: MVC, Actions and Controllers, Localization, CORS, most templates
Projects
None yet
Development

No branches or pull requests

6 participants