docs/en/Community-Articles/2024-09-18-Blazor-9-New-Features/post.md
In this article, I'll highlight .NET 9's Blazor updates and important features for ASP.NET Core 9.0. These features are based on the latest .NET 9 Preview 7.
There's a new solution template to create .NET MAUI native and Blazor web client apps. This new template allows to choose a Blazor interactive render mode, it uses a shared Razor class library to maintain the UI's Razor components.
For more info:
MapStaticAssetsThis new middleware optimizes the delivery of static assets in any ASP.NET Core app, also for Blazor. Basically it compresses assets via Gzip, fingerprints for all assets at build time with a Base64 and removes caches when Visual Studio Hot Reload (development time) is in action.
For more info:
For more info:
The ComponentBase.RendererInfo and ComponentBase.AssignedRenderMode now allows to detect the following actions:
RendererInfo.Name returns the location where the component is executingRendererInfo.IsInteractive indicates if the component supports interactivity at the time of rendering.ComponentBase.AssignedRenderMode exposes the component's assigned render modeWhen the previous app is disconnected and the user navigates to this app, or browser put this app in sleep mode, Blazor runs reconnection mechanism.
When reconnection is not successful because your server killed connection, it automatically makes a full page refresh.
With the new below config, you can adjust your reconnection retry time:
Blazor.start({
circuit: {
reconnectionOptions: {
retryIntervalMilliseconds: (previousAttempts, maxRetries) =>
previousAttempts >= maxRetries ? null : previousAttempts * 1000
},
},
});
The new APIs in ASP.NET make it easier to add authentication to existing Blazor Web Apps. These APIs, now part of the Blazor Web App project template, allow authentication state to be serialized on the server and deserialized in the browser, simplifying the process of integrating authentication. This removes the need for developers to manually implement or copy complex code, especially when using WebAssembly-based interactivity.
For more info:
With .NET 9, adding static server-side rendering (SSR) pages to globally interactive Blazor Web Apps has become simpler. The new [ExcludeFromInteractiveRouting] attribute allows developers to mark specific Razor component pages that require static SSR, such as those relying on HTTP cookies and the request/response cycle. Pages annotated with this attribute exit interactive routing and trigger a full-page reload, while non-annotated pages default to interactive rendering modes like InteractiveServer. This approach enables flexibility between static and interactive rendering depending on the page's requirements.
For more info:
Razor components support constructor injection, allowing services like NavigationManager to be injected directly into a component's constructor. This can be used to manage navigation actions, such as redirecting the user upon an event like a button click.
For more info:
By default, Interactive Server components enable WebSocket compression and set a frame-ancestors Content Security Policy (CSP) to self, restricting embedding the app in <iframe>. Besides, compression can be disabled to improve security by setting ConfigureWebSocketOptions to null, though this may reduce performance. To prevent embedding the app in any iframe while maintaining WebSocket compression, set the ContentSecurityFrameAncestorsPolicy to 'none'.
For more info:
KeyboardEventArgs.IsComposingThe new KeyboardEventArgs.IsComposing property indicates whether a keyboard event is part of a composition session, which is essential for properly handling international character input methods.
QuickGrid with new OverscanCount parameterThe QuickGrid component now includes an OverscanCount property, which controls how many extra rows are rendered before and after the visible area when virtualization is enabled. By default, OverscanCount is set to 3, but it can be adjusted as below to 5.
<QuickGrid ItemsProvider="itemsProvider" Virtualize="true" OverscanCount="5">...</QuickGrid>
InputNumber<TValue> ComponentThe InputNumber<TValue> component now supports the type="range" attribute, allowing for range inputs like sliders or dials. This feature supports model binding and form validation, offering a more interactive way to input numerical data compared to the traditional text box.
<EditForm>
<InputNumber @bind-Value="Model.ProductCount" max="999" min="1" step="1" type="range" />
</EditForm>
@code {
public class MyModel
{
[Required, Range(minimum: 1, maximum: 999)]
public int ProductCount { get; set; }
}
}