How do you preserve the state in Blazor server-side?

Blazor Server is a stateful application framework that maintains a connection to the server. Its state will occur in the server memory known as circuit. We can preserve the state in Blazor server-side three ways: Server-side storage URL Browser storage The following example demonstrates how to preserve the state in Blazor server-side using browser storage. Install the Blazored.SessionStorage NuGet package in the NuGet package manager to store the session data in Blazor. Add the Blazored SessionStorage configuration to the Blazor Server app. [Startup.cs] [Index.razor] Refer to this documentation for details.

How do I deploy a Blazor WebAssembly app to Heroku?

Heroku is a cloud platform as a service that enables you to deploy, build, and run applications in the cloud. Following are the steps to deploy a Blazor WebAssembly application to Heroku. Prerequisites: Blazor WebAssembly app in your GitHub Heroku account Deploy a Blazor WebAssembly app in Heroku Go to the Heroku dashboard and create a new app. Provide the app name and create an app. Now the new app is created. Next connect your GitHub Blazor WebAssembly app repository to Heroku for deployment. By default, Heroku does not support .NET apps for deployment. So, we add third-party build packs to enable the .NET app deployment support. Go to the settings tab and click Add buildpack. Now the dialog box will open. Copy the link below, paste it into the Buildpack URL section, and save your changes. The build pack has been added. Go to the Deploy tab and deploy the repository manually. The Blazor app starts building now. Now the Blazor app is deployed. Click View to run the deployed application. The deployed application opens in the default browser. See the following image for reference.

What are all the hosting models available in Blazor? Which Blazor hosting model should I choose and when?

There are two hosting models available in Blazor: Blazor Server and Blazor WebAssembly. Blazor Server Server-side Blazor apps are executed on the server within an ASP.NET Core application. All UI updates, JavaScript calls, and event handling are handled from the server using a SignalR connection. On the client side, the blazor.server.js script sets up the SignalR connection with the server. The script is served to the client-side app from an embedded resource in the ASP.NET Core shared framework. Blazor WebAssembly Blazor WebAssembly apps are executed on the client-side in the browser. The .NET runtime is downloaded with the app along with the app assembly and any required dependencies, then the application is executed directly from the browser UI thread. The WebAssembly app static assets are loaded as static files to a web server or service capable of serving static content to clients. The blazor.webassembly.js script handles downloading the app, the app’s dependencies, and the .NET runtime. The script file also initializes the runtime to run the app. Refer to this documentation section to help you choose a Blazor hosting model.

How do I cancel background long-running tasks when a user navigates to another page in Blazor?

The background long-running tasks are cancelled by using the CancellationToken object in Blazor. If an application downloads the data to render in view and then you navigate to another page, the CancellationToken cancels the download by using the Cancel() method. Call the Cancel()  method by using the Dispose() method to cancel the background running task. In the following example, the background running task in the FetchData.razor component is cancelled when a user navigates to other pages in Blazor.[FetchData.razor] Refer to this documentation for more details.

How do you initiate automatic logout when a user is inactive in Blazor?

Using JavaScript onmousemove and onkeypress events to trigger the Timer function in a Razor component can identify whether a user is active or inactive. If the user has not performed any onmousemove or onkeypress events, the user is considered in an inactive state and the Timer function is called after certain TimeInterval to log the user out. Follow these steps to log the user out automatically if they’re not active: Call the onmousemove and onkeypress properties in a JavaScript function to fire the Timer function in the Razor component. [_Host.cshtml] Now call the Timer method to identify whether the user is active or not. Add navigation to the logout action when the user is inactive.[MainLayout.razor]