Pages and responses
Render components, use MapInertia, and understand the root template and response status codes.
An Inertia page is a component name plus a set of props. The server writes the page object into the response; the client resolves the component and renders it.
Rendering a page
Render(component, props) returns an InertiaResult. The component name must match your client-side page resolver exactly, including case.
Render accepts an InertiaProps (an ordered string-keyed dictionary), any IDictionary<string, object?>, or a typed object whose properties are the props. Nested InertiaProps and dictionaries are walked at any depth.
Every page has an errors prop (an empty object when there are no errors). Key order is errors, shared props, then page props.
Per-response options
InertiaResult builds on the response without changing the component:
| Method | Effect |
|---|---|
.WithStatusCode(int) | Sets the HTTP status of the page response |
.WithRootView(string) | Uses another root template for this response |
.WithViewData(key, value) | Makes a value available to @viewData("key") |
.WithSsr(SsrMode) / .WithoutSsr() | Overrides the SSR mode for this response |
.WithSsrCache(duration, staleFor?) | Reuses a server-rendered response for the same page JSON |
Pages without a handler
MapInertia maps GET and HEAD to a rendered page, like Laravel's Route::inertia:
The root template
On the first visit the response is an HTML document. Inertia.Net reads app.html from the content root (configurable with RootView), parses it once, and replaces these tokens:
| Token | Output |
|---|---|
@inertia | Without SSR: the page JSON script and the root element. With SSR: the server-rendered body. Required. |
@inertiaHead | The SSR head tags joined with newlines. Empty without SSR. |
@vite("resources/js/app.tsx") | Vite tags for one or more comma-separated entries. |
@viteReactRefresh | The React Fast Refresh preamble while the dev server runs. Put it before @vite. |
@viewData("title") | An HTML-encoded value passed with WithViewData. |
@@ | A literal @. |
Other @ characters, such as CSS at-rules and e-mail addresses, are left alone. Arguments accept single or double quotes.
The page JSON is escaped with \uXXXX sequences, so a prop containing </script> cannot break out of the script element.
RootElementId (default app) changes both the root element id and the data-page value. The client finds the page script by that id.
Copy the template
app.html is read from disk at runtime. Add CopyToOutputDirectory and CopyToPublishDirectory so it is present after dotnet publish.
Razor root view (MVC)
With Inertia.Net.Mvc, the first-visit HTML can be a Razor view:
"App" finds Views/Shared/App.cshtml. The view receives an InertiaRootViewContext model and uses tag helpers:
<vite entry="a.ts, b.css" /> takes comma-separated entries. SSR runs at most once per request and is shared by <inertia-head /> and <inertia />. WithRootView("Other") takes Razor view names when the Razor root view is enabled.
A custom root view
Implement IInertiaRootView and register it before AddInertia:
Page URL and path base
The page object's url is the request PathBase, Path and QueryString. Behind a proxy that rewrites paths, set o.UrlResolver to return the URL the browser sees:
Back() and the Vite asset URLs also respect the path base.