...

/

Passing HTML Into Razor Components

Passing HTML Into Razor Components

Learn to pass any arbitrary HTML to Razor components.

Blazor allows us to pass any arbitrary HTML into Razor components. To demonstrate how this can be done, we have the following project setup. In this project, we are referencing a Razor component inside another Razor component and passing some arbitrary HTML into it.

<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>
A Blazor project setup with a RenderFragment example

Consuming arbitrary HTML by Razor components

We can only insert arbitrary HTML into Razor components that have been set ...