npx skills add ...
npx skills add dotnet/skills --skill fetch-and-send-data
Call APIs, load data into components, and handle the async lifecycle in Blazor. USE FOR fetching data from a backend, submitting data to an API, displaying loading/error states, registering HttpClient, building service abstractions for Auto/WebAssembly render modes. DO NOT USE for form validation (see collect-user-input), prerendering persistence (see support-prerendering), or project scaffolding (see create-blazor-project).
npx skills add dotnet/skills --skill fetch-and-send-data
Check Interactivity Mode and Scope:
| Mode | Data access |
|---|---|
| None (Static SSR) | Server-side: inject services/DbContext. Use [StreamRendering] for loading UX. |
| Server | Server-side: inject services/DbContext. Guard prerender with ??= + [PersistentState]. |
| WebAssembly | Browser-side: HttpClient only. No direct server access. |
| Auto | Both server and browser. Always go through an API. |
Only needed when calling external APIs from Server, or always for WebAssembly/Auto. Server components accessing their own database should inject DbContext or a service directly.
For WebAssembly/Auto with prerendering, register in both server and .Client Program.cs.
No error handling needed in the simplest case — wrap the component usage in <ErrorBoundary> at the parent/layout level to catch unhandled exceptions.
Without [StreamRendering], the user sees nothing until OnInitializedAsync completes:
Only affects Static SSR. No effect on interactive components.
Prerendering calls OnInitializedAsync twice. Skip the duplicate:
See the support-prerendering skill for details.
Use <ErrorBoundary> as the default error strategy. It provides a consistent error experience across all components without any per-component catch logic. Wrap component usage at the layout or parent level:
Non-cancellation exceptions (HttpRequestException, etc.) propagate to ErrorBoundary automatically — no catch blocks needed in the component.
ComponentBase silently swallows all OperationCanceledException — both self-initiated (disposal, parameter change) and external (HttpClient timeout). ErrorBoundary never sees them. This means:
Only add catch blocks when the component needs behavior ErrorBoundary can't provide — typically retries or timeout-specific messages. Even then, only catch what you need:
If the component also needs to handle general errors with a retry button instead of letting ErrorBoundary take over:
exception.Message — it may contain PII, connection strings, or internal details. Use hardcoded user-friendly messages.ILogger — the real exception goes to the logging pipeline.CancellationToken — pass it to every async call so work stops when the component cancels.When data depends on a route or query parameter that changes (e.g., navigating between /products/1 and /products/2), use OnParametersSetAsync with a guard to skip reloads for parameters that don't affect data.
Key details:
loadedCategoryId skips reloads when only UI parameters change.products on subsequent loads — keep existing data visible with an isLoading overlay.IAsyncDisposable cancels pending work when the user navigates away.Disable the submit button while saving to prevent duplicate requests. Show a saving indicator.
When components run in both server and browser (Auto mode, or WebAssembly with prerendering), abstract data access behind an abstract base class:
Register the appropriate implementation in each project's Program.cs. Components inject the abstract base class.
OnInitializedAsync.OnParametersSetAsync unless data depends on a changing parameter. Use OnInitializedAsync for initial loads.DbContext in WebAssembly/Auto components — no database in the browser.HttpClient — inject the service directly.exception.Message to users — PII risk. Log it, show a generic message.OperationCanceledException for self-cancellation — ComponentBase handles it.