Skip to content

Commit ef60066

Browse files
authored
Render Modes article improvements (#36140)
1 parent a3822ac commit ef60066

3 files changed

Lines changed: 15 additions & 16 deletions

File tree

aspnetcore/blazor/components/render-modes.md

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -385,6 +385,8 @@ In the following example, the render mode is set interactive SSR by adding `@ren
385385

386386
If using the preceding component in a Blazor Web App, place the component in the server project's `Components/Pages` folder. The server project is the solution's project with a name that doesn't end in `.Client`. When the app is running, navigate to `/render-mode-2` in the browser's address bar.
387387

388+
[!INCLUDE[](~/blazor/includes/closure-of-circuits.md)]
389+
388390
## Client-side rendering (CSR)
389391

390392
Client-side rendering (CSR) renders the component interactively on the client using Blazor WebAssembly. The .NET runtime and app bundle are downloaded and cached when the WebAssembly component is initially rendered. Components using CSR must be built from a separate client project that sets up the Blazor WebAssembly host.
@@ -817,14 +819,6 @@ In the preceding code, change the `{INTERACTIVE RENDER MODE}` placeholder to the
817819

818820
:::moniker-end
819821

820-
## Discover components from additional assemblies
821-
822-
Additional assemblies must be disclosed to the Blazor framework to discover routable Razor components in referenced projects. For more information, see <xref:blazor/fundamentals/routing#route-to-components-from-multiple-assemblies>.
823-
824-
## Closure of circuits when there are no remaining Interactive Server components
825-
826-
[!INCLUDE[](~/blazor/includes/closure-of-circuits.md)]
827-
828822
## Custom shorthand render modes
829823

830824
The `@rendermode` directive takes a single parameter that's a static instance of type <xref:Microsoft.AspNetCore.Components.IComponentRenderMode>. The `@rendermode` directive attribute can take any render mode instance, static or not. The Blazor framework provides the <xref:Microsoft.AspNetCore.Components.Web.RenderMode> static class with some predefined render modes for convenience, but you can create your own.
@@ -863,13 +857,7 @@ Alternatively, a single component instance can define a custom render mode via a
863857

864858
At the moment, the shorthand render mode approach is probably only useful for reducing the verbosity of specifying the `prerender` flag. The shorthand approach might be more useful in the future if additional flags become available for interactive rendering and you would like to create shorthand render modes with different combinations of flags.
865859

866-
## Service injection via a top-level imports file (`_Imports.razor`)
867-
868-
*This section only applies to Blazor Web Apps.*
869-
870-
A top-level imports file in the `Components` folder (`Components/_Imports.razor`) injects its references into all of the components in the folder hierarchy, which includes the `App` component (`App.razor`). The `App` component is always rendered statically even if [prerendering of a page component is disabled](xref:blazor/components/prerender#disable-prerendering). Therefore, injecting services via the top-level imports file results in resolving *two instances* of the service in page components.
871860

872-
To address this scenario, inject the service in a new imports file placed in the `Pages` folder (`Components/Pages/_Imports.razor`). From that location, the service is only resolved once in page components.
873861

874862
## Additional resources
875863

aspnetcore/blazor/fundamentals/dependency-injection.md

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,18 @@ In components derived from a base class, the [`@inject`](xref:mvc/views/razor#in
293293
@inherits CustomComponentBase
294294
```
295295

296+
:::moniker range=">= aspnetcore-8.0"
297+
298+
## Service injection via a top-level imports file (`_Imports.razor`)
299+
300+
*This section only applies to Blazor Web Apps.*
301+
302+
A top-level imports file in the `Components` folder (`Components/_Imports.razor`) injects its references into all of the components in the folder hierarchy, which includes the `App` component (`App.razor`). The `App` component is always rendered statically even if [prerendering of a page component is disabled](xref:blazor/components/prerender#disable-prerendering). Therefore, injecting services via the top-level imports file results in resolving *two instances* of the service in page components.
303+
304+
To address this scenario, inject the service in a new imports file placed in the `Pages` folder (`Components/Pages/_Imports.razor`). From that location, the service is only resolved once in page components.
305+
306+
:::moniker-end
307+
296308
## Use DI in services
297309

298310
Complex services might require additional services. In the following example, `DataAccess` requires the <xref:System.Net.Http.HttpClient> default service. [`@inject`](xref:mvc/views/razor#inject) (or the [`[Inject]` attribute](xref:Microsoft.AspNetCore.Components.InjectAttribute)) isn't available for use in services. *Constructor injection* must be used instead. Required services are added by adding parameters to the service's constructor. When DI creates the service, it recognizes the services it requires in the constructor and provides them accordingly. In the following example, the constructor receives an <xref:System.Net.Http.HttpClient> via DI. <xref:System.Net.Http.HttpClient> is a default service.
@@ -777,7 +789,6 @@ services.AddScoped<BlazorServiceAccessor>();
777789

778790
:::moniker range=">= aspnetcore-8.0"
779791

780-
* [Service injection via a top-level imports file (`_Imports.razor`) in Blazor Web Apps](xref:blazor/components/render-modes#service-injection-via-a-top-level-imports-file-_importsrazor)
781792
* <xref:fundamentals/dependency-injection>
782793
* [`IDisposable` guidance for Transient and shared instances](xref:fundamentals/dependency-injection#idisposable-guidance-for-transient-and-shared-instances)
783794
* <xref:mvc/views/dependency-injection>

aspnetcore/blazor/security/webassembly/index.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ The following authentication scenarios are covered in the <xref:blazor/security/
174174

175175
Apply the [`[Authorize]` attribute](xref:blazor/security/index#authorize-attribute) ([API documentation](xref:Microsoft.AspNetCore.Authorization.AuthorizeAttribute)) to each Razor component of the app using ***one*** of the following approaches:
176176

177-
* In the app's Imports file, add an [`@using`](xref:mvc/views/razor#using) directive for the <xref:Microsoft.AspNetCore.Authorization?displayProperty=fullName> namespace with an [`@attribute`](xref:mvc/views/razor#attribute) directive for the [`[Authorize]` attribute](xref:blazor/security/index#authorize-attribute).
177+
* In the app's imports file, add an [`@using`](xref:mvc/views/razor#using) directive for the <xref:Microsoft.AspNetCore.Authorization?displayProperty=fullName> namespace with an [`@attribute`](xref:mvc/views/razor#attribute) directive for the [`[Authorize]` attribute](xref:blazor/security/index#authorize-attribute).
178178

179179
`_Imports.razor`:
180180

0 commit comments

Comments
 (0)