Files
MyAiConfiguration/shared/rules/csharp/references/blazor.md
T
Julian LechnerandClaude Opus 5.5 ff33ea1327 docs(rules): add MAUI and Blazor rules, close C# gaps
New C# topic rules for .NET MAUI and Blazor, both centered on
performance and cross-platform reuse. MAUI covers compilation strategy
per platform (ReadyToRun, NativeAOT), trim and AOT safety, lean startup,
CollectionView and flat layouts, and per-platform conventions. Blazor
covers render mode cost, WebAssembly payload size and AOT trade-offs,
prerender state, rendering cost, and Razor class libraries shared across
web, WebAssembly, and Hybrid hosts. The C# index and global routing now
name both.

The existing C# rules gain the net11.0 default of C# 15, async void only
for event handlers, explicit StringComparison, invariant culture for
machine data, UTC or DateTimeOffset for stored times, ProblemDetails,
and ValidateOnStart. Duplicate authorization and raw SQL guidance in the
architecture rule is merged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-18 09:57:17 +01:00

3.6 KiB

C# Blazor

Apply these rules when building or changing Blazor components and apps. Performance and cross-platform reuse are the primary design goals. Check the target framework first: render modes, state persistence, and antiforgery behavior differ between .NET 8, 9, 10, and 11. Load the UI/UX rules for interface design and the shared web security rule for browser concerns.

Performance

  • Measure before optimizing: profile rendering, download size, and server memory in Release builds against a baseline.
  • Choose the render mode for its cost: static server rendering for content, interactive server where latency is low and connections are few, WebAssembly where offline use or server load matters, and Auto to combine fast first load with client execution. Each mode moves cost between download size, server memory, and network latency.
  • Keep WebAssembly payloads small: trim, lazy load rarely used assemblies with BlazorWebAssemblyLazyLoad, and serve compressed, fingerprinted assets. Enable AOT (RunAOTCompilation, with WasmStripILAfterAOT) only for CPU-heavy apps, because it speeds execution but enlarges the download.
  • Expect prerendered components to initialize twice. Persist prerendered state with PersistentComponentState, or [PersistentState] on .NET 10+, instead of repeating service and database calls.
  • Use streaming rendering ([StreamRendering]) to show content while slow data loads on static server rendering.
  • Render less: virtualize large lists with Virtualize, give list items a stable @key, avoid unnecessary StateHasChanged calls, override ShouldRender for components with costly subtrees, and avoid creating thousands of small component instances in a loop when plain markup would do.
  • Batch JavaScript interop. Avoid many fine-grained calls in loops, and pass small, serializable data.
  • On interactive server rendering, keep circuit state small, release large objects promptly, and avoid per-user caches that grow without bounds.

Cross-platform

  • Put shared UI in a Razor class library that works in every host: Blazor Web App, WebAssembly, and Blazor Hybrid in .NET MAUI (BlazorWebView). Keep host-specific code out of it.
  • Access platform features (device info, storage, files, notifications) through interfaces in the shared library, implemented per host and registered with dependency injection.
  • Write components that work in every render mode they may run in: no browser-only APIs before the first render, no assumption of an HTTP context, and JavaScript interop only in OnAfterRenderAsync(firstRender).
  • Treat all WebAssembly and Hybrid client code and data as public. Never ship secrets or rely on client-side authorization; AuthorizeView and [Authorize] only hide UI. Enforce authorization in the server APIs and data access the client calls.
  • Keep per-user state out of singleton services. On interactive server rendering, state lives in the user's circuit and is lost on disconnect; persist what must survive a reconnect or reload.
  • Keep app.UseAntiforgery() in the pipeline. EditForm adds the token automatically; add <AntiforgeryToken /> to plain HTML forms. Validate forms with a validator component and again on the server.
  • Dispose IJSObjectReference and DotNetObjectReference instances in DisposeAsync, catch JSDisconnectedException there for server circuits, and implement IDisposable or IAsyncDisposable to unsubscribe events and stop timers.
  • Never render untrusted content with MarkupString; let Razor encode it. Wrap risky regions in ErrorBoundary and keep detailed errors out of production.
  • Test every host and render mode you ship, including Hybrid on each MAUI target platform.