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.
@using Microsoft.AspNetCore.Http;
<button @onclick="@onClick"> Click </button>
@code {
[Inject]
protected IHttpContextAccessor httpContextAccessor { get; set; }
private void onClick()
{
var isPreRendering = !this.httpContextAccessor.HttpContext.Response.HasStarted;
}
}
The HttpContextAccessor service should be registered by calling the AddHttpContextAccessor method in the Startup.cs.
public class Startup
{
. . . . .
public void ConfigureServices(IServiceCollection services)
{
services.AddRazorPages();
services.AddServerSideBlazor();
services.AddSingleton<WeatherForecastService>();
services.AddHttpContextAccessor();
}
. . . . .
}
Share with