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

Blazor WASM Unable to load static html page from wwwroot folder

$
0
0

I have created static html page under wwwroot folder, which having it's own js and css.

While trying to hit url in incognito mode in browser https://baseURL/Terms.html it redirect to Terms.html for first time but once while App loaded in browser and after try to hit https://baseURL/Terms.html then it redirect to login page.

I also moved static files root folder but same output.

Seems like Terms.html page is rendering MainLayout.razor

I am using Blazor WebAssembly and have below code in App.razor

//App.razor<CascadingAuthenticationState><Router AppAssembly="@typeof(Program).Assembly"><Found Context="routeData"><AuthorizeRouteView RouteData="@routeData" DefaultLayout="@typeof(MainLayout)"><NotAuthorized><UnAuthorized /></NotAuthorized></AuthorizeRouteView></Found><NotFound><LayoutView Layout="@typeof(MainLayout)"><p>Sorry, there's nothing at this address.</p></LayoutView></NotFound></Router></CascadingAuthenticationState>

UnAuthorized.razor is component and written below code

@inject NavigationManager NavigationManager<MudGrid><MudItem xs="12"><MudGrid>            You are not authorized to access this page.</MudGrid></MudItem></MudGrid>@code {    [CascadingParameter]    private Task<AuthenticationState> AuthenticationStateTask { get; set; }    protected override async Task OnInitializedAsync()    {        var authenticationState = await AuthenticationStateTask;        if (authenticationState?.User?.Identity is null || !authenticationState.User.Identity.IsAuthenticated)        {            NavigationManager.NavigateTo("/login", true);        }        StateHasChanged();    }}

In MainLayout.cs written below code

<AuthorizeView><NotAuthorized><UnAuthorized /></NotAuthorized></AuthorizeView>

Tried below solution:In App.Razor implemented below code

<CascadingAuthenticationState><Router AppAssembly="@typeof(Program).Assembly"><Found Context="routeData">            @* Check if the requested URL ends with ".html" *@            @if (ShouldBypassAuthorization(NavigationManager.Uri))            {<StaticFileView PagePath="@NavigationManager.Uri"></StaticFileView>            }            else            {<AuthorizeRouteView RouteData="@routeData" DefaultLayout="@typeof(MainLayout)"><NotAuthorized><UnAuthorized /></NotAuthorized></AuthorizeRouteView>            }</Found><NotFound><LayoutView Layout="@typeof(MainLayout)"><p>Sorry, there's nothing at this address.</p></LayoutView></NotFound></Router></CascadingAuthenticationState>   @code {        [Inject]        protected NavigationManager NavigationManager { get; set; }        // Method to check if the requested URL ends with ".html"        bool ShouldBypassAuthorization(string url)        {            return url != null && url.EndsWith(".html", StringComparison.OrdinalIgnoreCase);        }}

Created razor component StaticHtmlPage.razor

@code {    [Parameter]    public string PagePath { get; set; }}<iframe src="@PagePath" frameborder="0" style="width: 100%; height: 100vh;"></iframe>

Also Tried by creating separate layout name as PublicLayout and new Razor component using PublicLayout, it successfully rendered the store the HTML Content into htmlContent string but multiple css and JS conflicted

 @layout PublicLayout    @inject HttpClient HttpClient;    @page "/TermsTest"    @using System.IO    @inject IJSRuntime JSRuntime    @attribute [AllowAnonymous]    @if (!string.IsNullOrEmpty(htmlContent))    {<div @innerHTML="htmlContent"></div>    }    @((MarkupString)htmlContent)    @code{        private string htmlContent;        protected override async Task OnInitializedAsync()        {             htmlContent = await HttpClient.GetStringAsync("Terms.html");            Console.WriteLine(htmlContent);        }

Viewing all articles
Browse latest Browse all 4839

Trending Articles



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