【问题标题】:aspnetcore blazor navigation from razor component to razor page从 razor 组件到 razor 页面的 aspnetcore blazor 导航
【发布时间】:2020-05-23 00:19:05
【问题描述】:

我正在尝试从 razor 组件重定向到 razor 页面。如果用户未获得授权,我想从当前的 razor 组件重定向到 Login Razor 页面。

我已经重定向到登录组件

public class RedirectToLogin : ComponentBase
    {
        [Inject]
        protected NavigationManager NavigationManager { get; set; }

        protected override void OnInitialized()
        {
            NavigationManager.NavigateTo("./Identity/Account/Login",true);
        }
    }

这行抛出一个错误NavigationManager.NavigateTo("./Identity/Account/Login");

Microsoft.AspNetCore.Components.NavigationException: 'Microsoft.AspNetCore.Components.NavigationException' 类型的异常被抛出。'

我的假设是问题是从 razor 组件路由到 razor 页面。

【问题讨论】:

  • 我有同样的例外。您如何解决此错误?
  • @cickness 发布了我们接受的解决方案的答案。

标签: asp.net-core asp.net-identity razor-pages blazor-server-side


【解决方案1】:

我使用下面的路由器重定向到了一个未经授权的页面。

<Router AppAssembly="@typeof(Program).Assembly">
    <Found Context="routeData">
        <AuthorizeRouteView  RouteData="@routeData" DefaultLayout="@typeof(MainLayout)">
            <NotAuthorized>
                <div class="container">
            <h1>Sorry</h1>
            <p>You're not authorized to reach this page.</p>
            <p>You may need to log in as a different user.</p>
            <a href="Identity/Account/Login">Log in</a>
        </div>
                @*<RedirectToLogin />*@
            </NotAuthorized>
            <Authorizing>
                <div class="container">
                    <h1>Authentication in progress</h1>
                    <p>Leave your hands on the keyboard,  scanning your fingerprints.  Ain't technology grand.</p>
                </div>
            </Authorizing>
        </AuthorizeRouteView>
    </Found>
    <NotFound>
        <CascadingAuthenticationState>
            <LayoutView Layout="@typeof(MainLayout)">
                <p>Sorry, there's nothing at this address.</p>
            </LayoutView>
        </CascadingAuthenticationState>
    </NotFound>
</Router>

这对于现在的应用程序来说已经足够了。

【讨论】:

    猜你喜欢
    • 2021-06-07
    • 2019-09-18
    • 2021-05-10
    • 2021-11-10
    • 1970-01-01
    • 2021-04-22
    • 2018-07-18
    • 2023-02-08
    • 2019-07-31
    相关资源
    最近更新 更多