Use ComponentTagHelper parameters on Blazor Server for passing state to allow pre-rendering to function properly ( ComponentTagHelper parameters do not work on Blazor WebAssembly - likely a .NET 5 bug )

This commit is contained in:
Shaun Walker 2021-09-23 17:16:51 -04:00
parent c1b482e0c0
commit 005843ef2d
4 changed files with 45 additions and 30 deletions

View File

@ -4,37 +4,44 @@
@if (_initialized)
{
<div style="@_display">
@if (!_installation.Success)
@if (!_installation.Success)
{
<Installer />
}
else
{
@if (string.IsNullOrEmpty(_installation.Message))
{
<Installer />
}
else
{
@if (string.IsNullOrEmpty(_installation.Message))
{
<div style="@_display">
<CascadingAuthenticationState>
<CascadingValue Value="@PageState">
<SiteRouter Runtime="@Runtime" RenderMode="@RenderMode" OnStateChange="@ChangeState" />
</CascadingValue>
</CascadingAuthenticationState>
}
else
{
<div class="app-alert">
@_installation.Message
</div>
}
</div>
}
</div>
else
{
<div class="app-alert">
@_installation.Message
</div>
}
}
}
@code {
[Parameter]
public string AntiForgeryToken { get; set; }
[Parameter]
public string Runtime { get; set; }
[Parameter]
public string RenderMode { get; set; }
private bool _initialized = false;
private string _display = "display: none;";
private Installation _installation = new Installation { Success = false, Message = "" };
private string Runtime = "";
private string RenderMode = "";
private PageState PageState { get; set; }
@ -44,6 +51,7 @@
if (_installation.Alias != null)
{
SiteState.Alias = _installation.Alias;
SiteState.AntiForgeryToken = AntiForgeryToken;
}
else
{
@ -56,10 +64,15 @@
{
if (firstRender)
{
var interop = new Interop(JSRuntime);
SiteState.AntiForgeryToken = await interop.GetElementByName(Constants.RequestVerificationToken);
Runtime = await interop.GetElementByName("app_runtime");
RenderMode = await interop.GetElementByName("app_rendermode");
if (string.IsNullOrEmpty(AntiForgeryToken))
{
// parameter values are not set when running on WebAssembly (seems to be a .NET 5 bug) - need to retrieve using JSInterop
var interop = new Interop(JSRuntime);
AntiForgeryToken = await interop.GetElementByName(Constants.RequestVerificationToken);
SiteState.AntiForgeryToken = AntiForgeryToken;
Runtime = await interop.GetElementByName("app_runtime");
RenderMode = await interop.GetElementByName("app_rendermode");
}
_display = "";
StateHasChanged();
}

View File

@ -55,7 +55,7 @@
protected override async Task OnParametersSetAsync()
{
if (PageState == null && !string.IsNullOrEmpty(Runtime))
if (PageState == null)
{
await Refresh();
}

View File

@ -20,7 +20,7 @@
<body>
@(Html.AntiForgeryToken())
<app>
<component type="typeof(Oqtane.App)" render-mode="@Model.RenderMode" />
<component type="typeof(Oqtane.App)" render-mode="@Model.RenderMode" param-AntiForgeryToken="@Model.AntiForgeryToken" param-Runtime="@Model.Runtime" param-RenderMode="@Model.RenderMode.ToString()" />
</app>
<div id="blazor-error-ui">

View File

@ -12,6 +12,7 @@ using Oqtane.Repository;
using Microsoft.AspNetCore.Localization;
using Microsoft.Extensions.Configuration;
using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.AspNetCore.Antiforgery;
namespace Oqtane.Pages
{
@ -21,19 +22,18 @@ namespace Oqtane.Pages
private readonly ITenantManager _tenantManager;
private readonly ILocalizationManager _localizationManager;
private readonly ILanguageRepository _languages;
private readonly IAntiforgery _antiforgery;
public HostModel(
IConfiguration configuration,
ITenantManager tenantManager,
ILocalizationManager localizationManager,
ILanguageRepository languages)
public HostModel(IConfiguration configuration, ITenantManager tenantManager, ILocalizationManager localizationManager, ILanguageRepository languages, IAntiforgery antiforgery)
{
_configuration = configuration;
_tenantManager = tenantManager;
_localizationManager = localizationManager;
_languages = languages;
_antiforgery = antiforgery;
}
public string AntiForgeryToken = "";
public string Runtime = "Server";
public RenderMode RenderMode = RenderMode.Server;
public string HeadResources = "";
@ -41,6 +41,8 @@ namespace Oqtane.Pages
public void OnGet()
{
AntiForgeryToken = _antiforgery.GetAndStoreTokens(HttpContext).RequestToken;
if (_configuration.GetSection("Runtime").Exists())
{
Runtime = _configuration.GetSection("Runtime").Value;