Skip to content

[ASP.NET] Scoped CSS should URL encode the link header #51038

@javiercn

Description

@javiercn

With a project created like this

dotnet new blazor -ai -int webassembly -o 项目

Failing to do so causes a runtime error as follows:

fail: Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware[1]
      An unhandled exception has occurred while executing the request.
      System.InvalidOperationException: Invalid non-ASCII or control character in header: 0x9879
         at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.HttpHeaders.ThrowInvalidHeaderCharacter(Char ch)
         at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.HttpHeaders.ValidateHeaderValueCharacters(String headerCharacters, Boolean requireAscii)
         at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.HttpHeaders.ValidateHeaderValueCharacters(String headerName, StringValues headerValues, Func`2 encodingSelector)
         at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.HttpResponseHeaders.SetValueFast(String key, StringValues value)
         at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.HttpHeaders.Microsoft.AspNetCore.Http.IHeaderDictionary.set_Item(String key, StringValues value)
         at Microsoft.AspNetCore.Http.ParsingHelpers.AppendHeaderUnmodified(IHeaderDictionary headers, String key, StringValues values)
         at Microsoft.AspNetCore.StaticAssets.StaticAssetsInvoker.ApplyResponseHeadersAsync(StaticAssetInvocationContext context, Int32 statusCode)
         at Microsoft.AspNetCore.StaticAssets.StaticAssetsInvoker.SendAsync(StaticAssetInvocationContext context)
         at Microsoft.AspNetCore.StaticAssets.StaticAssetsInvoker.Invoke(HttpContext context)
         at Microsoft.AspNetCore.Builder.StaticAssetDevelopmentRuntimeHandler.<>c__DisplayClass3_0.<<AttachRuntimePatching>b__0>d.MoveNext()
      --- End of stack trace from previous location ---
         at Microsoft.AspNetCore.Authorization.AuthorizationMiddleware.Invoke(HttpContext context)
         at Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddlewareImpl.Invoke(HttpContext context)

This happens because of the Link header we attach for the bundle. Encoding the problematic characters fixes the issue

{
    "Name": "Link",
    "Value": "<%E9%A1%B9%E7%9B%AE.Client.74oq3j2vft.bundle.scp.css>; rel=\"preload\"; as=\"style\""
}

Metadata

Metadata

Assignees

Labels

Area-AspNetCoreRazorSDK, BlazorWebAssemblySDK, dotnet-watch

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions