How do I detect a prerendering app in Blazor?
You can use the IHttpContextAccessor.HttpContext.Response.HasStarted property to check whether the application is pre-rendering or not. HasStarted specifies that the response header has been sent to the client. If HasStarted is set to false, it means that the application is still pre-rendering and client connection is not yet established. Refer to the following code sample. The HttpContextAccessor service should be registered by calling the AddHttpContextAccessor method in the Startup.cs.
How can I avoid memory leaks when using JSInterop call?
To avoid memory leaks, dispose the DotNetObjectReference instance created by a component properly and allow proper garbage collection. The DotNetObjectReference instance should be disposed either at component side or JavaScript side. Use the following patterns to dispose DotNetObjectReference at the component level. Implement IDisposable interface in the component. Add Dispose method and dispose DotNetObjectReference object inside the Dispose method. When a component doesn’t dispose the DotNetObjectReference instance, it should be disposed at the JavaScript side as follows.
How do I access the appsettings in Blazor WebAssembly?
Configurations loaded by the Blazor WebAssembly can be accessed by injecting IConfiguration services. Using IConfiguration, the values from wwwroot/appsettings.json and wwwroot/appsettings.{ENVIRONMENT}.json files can be accessed. wwwroot/appsettings.json Index.razor More information about configuration can be found here.
How do I conditionally add Blazor components’ template?
The if statement is used to display a component or fragment conditionally. If the condition evaluates to true, the component will be loaded to the view. Whenever the properties used in the if statement changes, the condition evaluates to either true or false and adds or removes the component to the page. Refer to the following code sample.
How do I get/render the raw HTML from Blazor components?
In Blazor, you can render raw HTML from a component using the MarkupString type. The MarkupString type allows you to render HTML content directly without being sanitized or escaped by the framework. Here’s how you can use MarkupString to render raw HTML in a Blazor component: View Sample in GitHub