Quantcast
Channel: Active questions tagged blazor - Stack Overflow
Viewing all articles
Browse latest Browse all 4839

Blazor Render Component Fails

$
0
0

I am rendering a Blazor component as follows:

private async Task PrintComponent(){    var htmlout = await BlazorRenderer.RenderComponent<BillView>();    await JSRuntime.InvokeVoidAsync("printComponent1", htmlout);}

The BillView component is as follows:

<AuthorizeView Roles="Admin, Reception"><Authroized><p> This is a bill.</p></Authroized><NotAuthorized>  <p>You are not authorized.</p></NotAuthorized>

When I do not use the <AuthorizeView>, BillView renders and I can print.When I use <AuthorizeView> I get this error on:

var htmlout = await BlazorRenderer.RenderComponent<BillView>();

System.InvalidOperationException: Authorization requires a cascading parameter of type Task. Consider using CascadingAuthenticationState to supply this. at Microsoft.AspNetCore.Components.Authorization.AuthorizeViewCore.OnParametersSetAsync() at Microsoft.AspNetCore.Components.ComponentBase.CallStateHasChangedOnAsyncCompletion(Task task) at Microsoft.AspNetCore.Components.ComponentBase.RunInitAndSetParametersAsync() at Microsoft.AspNetCore.Components.Rendering.ComponentState.SupplyCombinedParameters(ParameterView directAndCascadingParameters) at Microsoft.AspNetCore.Components.RenderTree.RenderTreeDiffBuilder.InitializeNewSubtree(DiffContext& diffContext, Int32 frameIndex) at Microsoft.AspNetCore.Components.RenderTree.RenderTreeDiffBuilder.InsertNewFrame(DiffContext& diffContext, Int32 newFrameIndex) at Microsoft.AspNetCore.Components.RenderTree.RenderTreeDiffBuilder.AppendDiffEntriesForRange(DiffContext& diffContext, Int32 oldStartIndex, Int32 oldEndIndexExcl, Int32 newStartIndex, Int32 newEndIndexExcl) at Microsoft.AspNetCore.Components.Rendering.ComponentState.RenderIntoBatch(RenderBatchBuilder batchBuilder, RenderFragment renderFragment, Exception& renderFragmentException) at Microsoft.AspNetCore.Components.RenderTree.Renderer.ProcessRenderQueue() --- End of stack trace from previous location --- at Microsoft.AspNetCore.Components.RenderTree.Renderer.ProcessRenderQueue() at Microsoft.AspNetCore.Components.ComponentBase.StateHasChanged() at Microsoft.AspNetCore.Components.ComponentBase.RunInitAndSetParametersAsync() at Microsoft.AspNetCore.Components.Rendering.ComponentState.SupplyCombinedParameters(ParameterView directAndCascadingParameters) at Microsoft.AspNetCore.Components.Rendering.ComponentState.SetDirectParameters(ParameterView parameters) at Microsoft.AspNetCore.Components.RenderTree.Renderer.RenderRootComponentAsync(Int32 componentId, ParameterView initialParameters) at Microsoft.AspNetCore.Components.HtmlRendering.Infrastructure.StaticHtmlRenderer.BeginRenderingComponent(IComponent component, ParameterView initialParameters) at Microsoft.AspNetCore.Components.Web.HtmlRenderer.RenderComponentAsync(Type componentType, ParameterView parameters) at BlazorRenderer.<>c__DisplayClass4_01.<b__0>d.MoveNext() in C:\Users\hp\source\repos\dajma00\Clinic\Clinic\Models\BlazorRenderer.cs:line 28 --- End of stack trace from previous location --- at Microsoft.AspNetCore.Components.Rendering.RendererSynchronizationContext.<>c__111.<b__11_0>d.MoveNext() --- End of stack trace from previous location --- at Clinic.Pages.PatientOpen.PrintComponent() in C:\Users\hp\source\repos\dajma00\Clinic\Clinic\Pages\PatientOpen.razor:line 221 at

BlazorRenderer:

//from Andrew Lock. Makes it easy to use htmlRenderer. https://andrewlock.net/exploring-the-dotnet-8-preview-rendering-blazor-components-to-a-string/internal class BlazorRenderer{    private readonly HtmlRenderer _htmlRenderer;    public BlazorRenderer(HtmlRenderer htmlRenderer)    {        _htmlRenderer = htmlRenderer;    }    // Renders a component T which doesn't require any parameters    public Task<string> RenderComponent<T>() where T : IComponent        => RenderComponent<T>(ParameterView.Empty);    // Renders a component T using the provided dictionary of parameters    public Task<string> RenderComponent<T>(Dictionary<string, object?> dictionary) where T : IComponent        => RenderComponent<T>(ParameterView.FromDictionary(dictionary));    private Task<string> RenderComponent<T>(ParameterView parameters) where T : IComponent    {        // Use the default dispatcher to invoke actions in the context of the         // static HTML renderer and return as a string        return _htmlRenderer.Dispatcher.InvokeAsync(async () =>        {            HtmlRootComponent output = await _htmlRenderer.RenderComponentAsync<T>(parameters);            return output.ToHtmlString();        });    }}

Viewing all articles
Browse latest Browse all 4839

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>