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

View File

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

View File

@ -20,7 +20,7 @@
<body> <body>
@(Html.AntiForgeryToken()) @(Html.AntiForgeryToken())
<app> <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> </app>
<div id="blazor-error-ui"> <div id="blazor-error-ui">

View File

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