dotnet / razor

Compiler and tooling experience for Razor ASP.NET Core apps in Visual Studio, Visual Studio for Mac, and VS Code.
https://asp.net
MIT License
500 stars 191 forks source link

Regression in cshtml tilde URL handling in .NET 8 (UrlResolutionTagHelper is not used) #8884

Closed SteveSandersonMS closed 1 year ago

SteveSandersonMS commented 1 year ago

In the latest .NET 8 SDKs, we seem to have lost support for ~-prefixed URLs in .cshtml files. Among other problems, this will break existing Blazor Server apps.

Repro

  1. Ensure you're using the .NET 7 SDK. Use global.json or whatever you like to ensure that dotnet --version returns a v7 number.
  2. Create a Blazor Server app (dotnet new blazorserver). Run it and see that it works. If you check the HTML source, you'll see it contains <base href="/"> which is correct.
  3. Now change to the .NET 8 SDK (for example by updating the number in global.json). You can delete bin/obj if you want.
  4. Run the app now and see the layout is all broken. If you check the HTML source, you'll see it contains <base href="~/"> which is incorrect (note the extra tilde which should have been resolved on the server).

Further investigation

If you have a .cshtml file containing just the following:

@page "/"
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
<a href="~/hello">Hello</a>

... then with .NET 7 it compiles as:

        public async override global::System.Threading.Tasks.Task ExecuteAsync()
        {
            __tagHelperExecutionContext = __tagHelperScopeManager.Begin("a", global::Microsoft.AspNetCore.Razor.TagHelpers.TagMode.StartTagAndEndTag, "82176862633516880c14e9c83eb1e6a7f89dfb1a6671f09513b882b3c813fea63105", async() => {
                WriteLiteral("Hello");
            }
            );
            __Microsoft_AspNetCore_Mvc_Razor_TagHelpers_UrlResolutionTagHelper = CreateTagHelper<global::Microsoft.AspNetCore.Mvc.Razor.TagHelpers.UrlResolutionTagHelper>();
            __tagHelperExecutionContext.Add(__Microsoft_AspNetCore_Mvc_Razor_TagHelpers_UrlResolutionTagHelper);
            __tagHelperExecutionContext.AddHtmlAttribute(__tagHelperAttribute_0);
            await __tagHelperRunner.RunAsync(__tagHelperExecutionContext);
            if (!__tagHelperExecutionContext.Output.IsContentModified)
            {
                await __tagHelperExecutionContext.SetOutputContentAsync();
            }
            Write(__tagHelperExecutionContext.Output);
            __tagHelperExecutionContext = __tagHelperScopeManager.End();
            WriteLiteral("\r\n");
        }

... whereas with .NET 8 it compiles as:

        public async override global::System.Threading.Tasks.Task ExecuteAsync()
        {
            WriteLiteral("<a href=\"~/hello\">Hello</a>\r\n");
        }

... and so it's clearly not resolving the ~ in the href attribute value.

halter73 commented 1 year ago

This is blocking aspnetcore's SDK update. https://github.com/dotnet/aspnetcore/pull/49034#issuecomment-1608571858. It's already been tracked down as a regression caused by https://github.com/dotnet/razor/pull/8857 and both https://github.com/dotnet/razor/pull/8871 and https://github.com/dotnet/razor/pull/8877 are proposed fixes. @jjonescz

SteveSandersonMS commented 1 year ago

Thanks for the extra info. Razor folks, please feel free to close this issue if it's duplicating something else that tracks this.

jjonescz commented 1 year ago

Fixed by https://github.com/dotnet/razor/pull/8871.

jaredpar commented 1 year ago

@surayya-MS why did you re-open this issue? Do you believe the fix here did not fix it?

surayya-MS commented 1 year ago

Sorry, it was a mistake. Closing the issue.