简体   繁体   English

关于托管 Blazor WASM 的建议 - 当应用程序隐藏在登录页面后面时,初始加载前出现空白屏幕

[英]Advice on hosted Blazor WASM - blank screen before initial load when app is hidden behind a login page

I've created an app with Blazor WASM (.net 6) which is ASP.NET Core hosted with authentication.我已经使用 Blazor WASM (.net 6) 创建了一个应用程序,它是 ASP.NET 核心托管与身份验证。 When first accessing the app the user is presented with a login page and cannot get to the rest of the app until signed in.首次访问应用程序时,用户会看到一个登录页面,并且在登录之前无法访问应用程序的 rest。

I'm having a slight issue where when the app initially loads, it briefly shows the loading message before displaying a blank screen for a couple of seconds and then finally the login page.我有一个小问题,当应用程序最初加载时,它会短暂显示加载消息,然后显示空白屏幕几秒钟,然后最后显示登录页面。 I want to get rid of that blank screen that appears inbetween the 'loading...' and login and suspect it's something to do with the way I've adjusted the boilerplate code to force login before access.我想摆脱出现在“加载...”和登录之间的空白屏幕,并怀疑这与我调整样板代码以在访问前强制登录的方式有关。

This is my index.html:这是我的索引。html:

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />
    <title>TimesheetExpense</title>
    <base href="/" />
    [various links/scripts]
</head>

<body>
    <div id="app">
        <div class="gradient">
            <div class="container">
                <div class="row">
                    <div class="col-12">
                        <center><p class="text-white">Loading...</p></center>
                    </div>
                </div>
            </div>
        </div>
    </div>
    <div id="blazor-error-ui">
        An unhandled error has occurred.
        <a href="" class="reload">Reload</a>
        <a class="dismiss">🗙</a>
    </div>
    <script src="_content/Microsoft.AspNetCore.Components.WebAssembly.Authentication/AuthenticationService.js"></script>
    <script src="_framework/blazor.webassembly.js"></script>
    <script>navigator.serviceWorker.register('service-worker.js');</script>
</body>

</html>

This is my App.razor:这是我的 App.razor:

<CascadingAuthenticationState>
    <LoadingScreen>
        <Router AppAssembly="@typeof(App).Assembly">
            <Found Context="routeData">
                <AuthorizeRouteView RouteData="@routeData" DefaultLayout="@typeof(MainLayout)">
                    <NotAuthorized>
                        @if (context.User.Identity?.IsAuthenticated != true)
                        {
                            <RedirectToLogin />
                        }
                        else
                        {
                            <p role="alert">You are not authorized to access this resource.</p>
                        }
                    </NotAuthorized>
                </AuthorizeRouteView>
                <FocusOnNavigate RouteData="@routeData" Selector="h1" />
            </Found>
            <NotFound>
                <PageTitle>Not found</PageTitle>
                <LayoutView Layout="@typeof(MainLayout)">
                    <p role="alert">Sorry, there's nothing at this address.</p>
                </LayoutView>
            </NotFound>
        </Router>
    </LoadingScreen>
</CascadingAuthenticationState>

This is my Index.razor:这是我的 Index.razor:

@page "/"

@using TimesheetExpense.Client.Features.Dashboard

<PageTitle>Timesheet Expense</PageTitle>

<AuthorizeView>
    <Authorized>
        <Dashboard />
    </Authorized>
    <NotAuthorized>
        <Login />
    </NotAuthorized>
</AuthorizeView>

I've tried setting up prerendering (which was a mission in itself because I'm using authentication and other services that needed setting up server side in order for it not to error) but this just cuts out the 'loading...' screen.我已经尝试设置预渲染(这本身就是一项任务,因为我正在使用身份验证和其他需要设置服务器端的服务才能使其不会出错)但这只是切断了“正在加载...”屏幕. The login page appears instantly, but then disappears for a couple of seconds (the blank screen returning) and then reappears.登录页面立即出现,然后消失几秒钟(返回空白屏幕),然后重新出现。

I've also tried adding <Authorizing></Authorizing> with a custom message in both my App.razor and Index.razor files to see if that displays instead of the blank screen but nothing happens.我还尝试在我的 App.razor 和 Index.razor 文件中添加带有自定义消息的<Authorizing></Authorizing> ,以查看是否显示该消息而不是空白屏幕,但没有任何反应。

Try with this App.razor code:试试这个App.razor代码:

<Router AppAssembly="typeof(App).Assembly">
<Found Context="routeData">
    <AuthorizeRouteView RouteData="routeData" DefaultLayout="typeof(MainLayout)" />
</Found>
<NotFound>
    <LayoutView Layout="typeof(MainLayout)">
        <h1>Page not found</h1>
        <p>Sorry, but there's nothing here!</p>
    </LayoutView>
</NotFound>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 blazor 托管 wasm 本地身份 - 脚手架登录页面上的错误 - blazor hosted wasm local identity - error on scaffold login page Blazor WASM 未重定向到登录页面 - Blazor WASM Not Redirecting to Login Page 是否可以将环境变量从主机传递到托管的 blazor wasm 应用程序? - Is it possible to pass environment variables from the host to a hosted blazor wasm app? 在红隼中托管 blazor wasm asp.net 核心托管应用 - Hosting blazor wasm asp.net core hosted app in kestrel Blazor 使用 nginx 在 docker 中托管时缺少 WASM 样式 - Blazor WASM styling missing when hosted in docker using nginx 如何在 Visual Studio 中调试服务器托管的 Blazor WASM 代码隐藏文件? - How can you debug server-hosted Blazor WASM code behind files in Visual Studio? Blazor WASM 页面路由 - Blazor WASM Page Routing Blazor Wasm Hosted Prerender with Authentication AuthenticationStateProvider 未注册 - Blazor Wasm Hosted Prerender with Authentication AuthenticationStateProvider not registered Blazor WASM 托管如何在客户端页面中列出所有身份用户? - Blazor WASM hosted how can I list all Identity Users in a Client page? Blazor WASM 应用程序中“托管 ASP.NET Core”的目的是什么? - What's the purpose of "ASP.NET Core hosted" in Blazor WASM app?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM