Skip to content

Trim warnings from default <LayoutView> usage in Blazor Router / NotFound when AOT is enabled #61358

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

Closed
1 task done
Jackhammer88 opened this issue Apr 7, 2025 · 1 comment
Labels
area-blazor Includes: Blazor, Razor Components NativeAOT

Comments

@Jackhammer88
Copy link

Is there an existing issue for this?

  • I have searched the existing issues

Describe the bug

Using default Razor syntax with and (as generated in standard Blazor templates) triggers trimming and AOT warnings when publishing with PublishTrimmed=true and PublishAot=true.

For example, this code:

<Router AppAssembly="@typeof(App).Assembly">
    <Found Context="routeData">
        <RouteView RouteData="@routeData" DefaultLayout="@typeof(MainLayout)" />
        <FocusOnNavigate RouteData="@routeData" Selector="h1" />
    </Found>
    <NotFound>
        <PageTitle>Not found</PageTitle>
        <LayoutView Layout="@typeof(MainLayout)">
            <p role="alert">Sorry, there's nothing at this address.</p>
        </LayoutView>
    </NotFound>
</Router>

produces trimming warnings such as:

IL2110: Blazor.UI.App.<>c.<BuildRenderTree>b__0_1(RenderTreeBuilder): Field 'Microsoft.AspNetCore.Components.LayoutView.<Layout>k__BackingField' with 'DynamicallyAccessedMembersAttribute' is accessed via reflection.
IL2111: LayoutView.WrapInLayout(Type, RenderFragment) is accessed via reflection and can't guarantee trim-safety.

These warnings come from the standard boilerplate code, and there is no obvious way to annotate the Razor or suppress the reflection usage safely without manually refactoring the rendering logic.

Expected Behavior

The generated templates and idiomatic Razor code should either:

  • Avoid unsafe reflection, or
  • Automatically preserve required types/members (e.g., via linker config or DynamicDependency)
  • Or at least clearly document the required workaround

Steps To Reproduce

  1. Create a Blazor WebAssembly app.
  2. Use in a NotFound section of .
  3. In .csproj set:
<PropertyGroup>
    <TargetFramework>net9.0</TargetFramework>
    <Nullable>enable</Nullable>
    <ImplicitUsings>enable</ImplicitUsings>
    <InvariantGlobalization>true</InvariantGlobalization>
    <RunAOTCompilation>true</RunAOTCompilation>
</PropertyGroup>
  1. Run dotnet publish -c Release

Exceptions (if any)

IL2110: Blazor.UI.App.<>c.b__0_1(RenderTreeBuilder): Field 'Microsoft.AspNetCore.Components.LayoutView.k__BackingField' with 'DynamicallyAccessedMembersAttribute' is accessed via reflection.
IL2111: LayoutView.WrapInLayout(Type, RenderFragment) is accessed via reflection and can't guarantee trim-safety.

.NET Version

9.0.202

Anything else?

Environment:
Arch linux x64

@ghost ghost added the area-blazor Includes: Blazor, Razor Components label Apr 7, 2025
@javiercn
Copy link
Member

javiercn commented Apr 7, 2025

@Jackhammer88 thanks for contacting us.

Blazor does not support AoT nor full trimming.

@javiercn javiercn closed this as completed Apr 7, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-blazor Includes: Blazor, Razor Components NativeAOT
Projects
None yet
Development

No branches or pull requests

2 participants