In a Blazor Web App, why does a button's @onclick never run on a page without @rendermode, and what fixes it?
answer
- static SSR keeps no component alive
- @rendermode InteractiveServer under @page
- AddInteractiveServerComponents on the services
- AddInteractiveServerRenderMode on MapRazorComponents
- Auto needs both Server and WebAssembly calls
basics
~20 sWithout a render mode the page is static SSR: the server sends finished HTML and discards the component, so nothing is left to run the handler. Give the page an interactive render mode, and make sure Program.cs registers that mode's services and endpoints.
solid answer
~40 sA Blazor Web App page with no `@rendermode` in its chain is **static SSR**: the server renders HTML for the request and throws the component away, so the browser has a button but no C# behind it - and no error is shown. The fix is to add `@rendermode InteractiveServer` (or `InteractiveWebAssembly` / `InteractiveAuto`) under `@page`, or on the instance that needs it. That only works if `Program.cs` enables the mode twice: `AddRazorComponents().AddInteractiveServerComponents()` on the services and `MapRazorComponents<App>().AddInteractiveServerRenderMode()` on the endpoints; WebAssembly uses the `...WebAssembly...` pair, and Auto needs both pairs. Leave one out and Blazor throws an `InvalidOperationException` naming the missing call.
code
csharp · 13 linesvar builder = WebApplication.CreateBuilder(args);
builder.Services.AddRazorComponents()
.AddInteractiveServerComponents();
var app = builder.Build();
app.UseAntiforgery();
app.MapStaticAssets();
app.MapRazorComponents<App>()
.AddInteractiveServerRenderMode();
app.Run();go deeper
Recall that the default render mode is static and that a missing @rendermode explains a button that does nothing without any error.
Explain the two halves of enabling a mode - services and endpoints - and what each AddInteractive call adds, including why Auto needs both pairs.
Recognise the two InvalidOperationException messages instantly, and choose between per-instance, per-page and global modes based on how much of the page truly needs interactivity.
Set a team convention for where render modes are declared so that interactivity is a deliberate, reviewable choice rather than something sprinkled on to fix dead buttons.
## Why the button is dead In a **Blazor Web App** every component has a **render mode**, and the default is **Static** - static server-side rendering, or **static SSR**. Under static SSR the server creates the page's components, renders them to HTML for this one HTTP request, writes the HTML to the response and then discards the components. The browser receives a perfectly normal `<button>` element. When the user clicks it nothing happens, because: - there is no server-side component instance left to call; - no circuit (the SignalR connection Interactive Server uses) was opened; - no .NET WebAssembly runtime was started in the browser. No exception is thrown and nothing is logged; the markup simply has no behaviour attached. This is a common first surprise for developers coming from the older Blazor Server template, where everything was interactive. The template hides the issue at first: with the default `--interactivity Server` it creates interactive pages per page, so its sample `Counter.razor` carries `@rendermode InteractiveServer`, while a page you add yourself does not. ## Fix 1: give the page or the component a mode 1. **On the page definition**, under `@page`: `@rendermode InteractiveServer`. The whole page and its children become interactive. 2. **On one component instance** inside a static page: `<LeaveRequestForm @rendermode="InteractiveServer" />`. The page stays static; only that widget becomes interactive - a good fit for a self-service portal page that is mostly static text with one interactive form. 3. **For the whole app**, on the `Routes` and `HeadOutlet` components in `App.razor`: `<Routes @rendermode="InteractiveServer" />`. The `--all-interactive` template option generates this. The short names need `@using static Microsoft.AspNetCore.Components.Web.RenderMode`, which the template places in `Components/_Imports.razor`; without it write `RenderMode.InteractiveServer`. ## Fix 2: register the mode in Program.cs An interactive mode needs two registrations, and they are easy to confuse: | Call | Where | What it adds | |---|---|---| | `AddRazorComponents()` | `builder.Services` | the services for Razor components, including static SSR | | `.AddInteractiveServerComponents()` | chained on `AddRazorComponents()` | the circuit services for Interactive Server | | `.AddInteractiveWebAssemblyComponents()` | chained on `AddRazorComponents()` | the services for WebAssembly components | | `MapRazorComponents<App>()` | `app` | endpoints for every routable component, with `App` as the root | | `.AddInteractiveServerRenderMode()` | chained on `MapRazorComponents` | the Blazor hub endpoint the circuit connects to | | `.AddInteractiveWebAssemblyRenderMode()` | chained on `MapRazorComponents` | endpoints serving the WebAssembly runtime and assemblies | Interactive Auto uses both runtimes, so it needs **both** service calls and **both** endpoint calls. WebAssembly and Auto components must also be compiled in the `.Client` project, whose assembly the template registers with `.AddAdditionalAssemblies(typeof(Client._Imports).Assembly)`. ## What happens when one call is missing - **Endpoint call missing:** a page with `@rendermode InteractiveServer` throws an `InvalidOperationException` while rendering, saying the component has a render mode but the required endpoints are not mapped, and telling you to add `AddInteractiveServerRenderMode` to `MapRazorComponents`. The WebAssembly variant names `AddInteractiveWebAssemblyRenderMode`. - **Service call missing:** chaining `AddInteractiveServerRenderMode()` without `AddInteractiveServerComponents()` fails when the Razor component endpoints are built, with an `InvalidOperationException` saying no provider exists for the render mode and that a call to `AddInteractiveServerComponents` or `AddInteractiveWebAssemblyComponents` is probably missing. Both messages name the missing method, which makes them quick to fix once you know the two halves exist. ## Pitfalls - `AddServerSideBlazor()` and `MapBlazorHub()` belong to the older Blazor Server hosting model; in a Blazor Web App the pair is `AddInteractiveServerComponents()` and `AddInteractiveServerRenderMode()`. - Making the root `App` component interactive with `@rendermode` at the top of `App.razor` is not supported; global interactivity goes on `Routes`. - A static page can still post data without interactivity: an `EditForm` with a `FormName` works under static SSR, so not every form needs a mode.
- Why does the template's Counter page work while your new page does not?With the default per-page interactivity the template writes `@rendermode InteractiveServer` at the top of `Counter.razor` (or `InteractiveAuto` / `InteractiveWebAssembly` in the client project, depending on `--interactivity`). Your page has no such line, so it falls back to the default Static mode. Global interactivity (`--all-interactive`) instead sets the mode once on `<Routes>` in `App.razor`, and every page inherits it.
- Which registrations does a page with @rendermode InteractiveAuto need?Both runtimes: `AddInteractiveServerComponents()` and `AddInteractiveWebAssemblyComponents()` on `AddRazorComponents()`, and `AddInteractiveServerRenderMode()` and `AddInteractiveWebAssemblyRenderMode()` on `MapRazorComponents<App>()`. The page itself must be compiled in the `.Client` project so the browser can download it, and the server must know that assembly through `AddAdditionalAssemblies`.
- Blazor components are always interactive; a dead button means the handler threw an exception.
- AddServerSideBlazor and MapBlazorHub are what a .NET 10 Blazor Web App needs for Interactive Server.
- Registering AddInteractiveServerComponents alone is enough; the endpoint call is optional.
- Putting @rendermode InteractiveServer at the top of App.razor is the way to make the whole app interactive.
- Interactive Auto needs only the WebAssembly registrations because it ends up in the browser.