NavigateTo 上的 NavigationError
NavigationError on NavigateTo
我正在试用 Blazor ServerSide,并创建了一个组件以在用户未登录时重定向到登录页面。
@inject Microsoft.AspNetCore.Components.NavigationManager NavigationManager;
@code {
/// <inheritdoc />
protected override Task OnInitializedAsync()
{
NavigationManager.NavigateTo("Login");
return Task.CompletedTask;
}
}
但总是在调用 "NavigateTo" 时抛出以下异常:
"Microsoft.AspNetCore.Components.NavigationException: Exception of type 'Microsoft.AspNetCore.Components.NavigationException' was thrown.
at Microsoft.AspNetCore.Components.Server.Circuits.RemoteNavigationManager.NavigateToCore(String uri, Boolean forceLoad)
at Microsoft.AspNetCore.Components.NavigationManager.NavigateTo(String uri, Boolean forceLoad)
at ApplySupportTool.Blazor.Pages.RedirectToLogin.OnInitializedAsync() in C:\Users\padruttn\Documents\git\ApplySupportTool\src\ApplySupportTool.Blazor\Pages\RedirectToLogin.razor:line 8"
有趣的是,尽管存在异常,但仍进行了导航。
我也尝试使用路径“/login”调用它,但这里的行为相同。
有一个 open issue on github for this problem. See also the preceeding issue,其中提到了一个可能的解决方法:将 NavigateTo
方法放入 OnAfterRender
而不是 OnInitialized
。
旧 post,但是 - 如果您是 运行 Blazor 服务器应用程序,则此行为仅在呈现模式为 "ServerPrerendered" 时发生。通过将模式更改为 "Server" 来禁用预渲染使得异常不会首先抛出:
<app>
<component type="typeof(App)" render-mode="Server" />
</app>
我搜索了当前的 Blazor 文档和更改说明,但没有发现任何提及,以防万一它对其他人有帮助...
当我试图从另一个线程调用 navigateto 时遇到了这个问题。 render-mode = "服务器" - 解决了问题
我需要将其放入 OnInitialized
而不是 OnAfterRender
,因此不得不使用 render-mode="Server"
方法,但显然不推荐这样做。
它还在 GitHub 问题中指出,这只发生在调试中,不会发生在发布中,因此一个折衷的选择是将 _Host.cshtml
更改为包含:
<environment include="Staging,Production">
<component render-mode="ServerPrerendered" type="typeof(App)" />
</environment>
<environment include="Development">
<component render-mode="Server" type="typeof(App)" />
</environment>
我 运行 遇到同样的问题并提交 issue #28355。官方的回答是,NaviagteTo
放在OnInitialized
时,忽略异常是省事的。以下是javiercn的回答:
Yes, it is completely safe to ignore it. The debugger stops because it is configured to do so, but in this case the exception is always handled. You can turn-off breaking on this exception if it's caught on the debugger settings.
Issue #13582 处理如何防止调试器在此异常处停止。
请注意,以下可能被视为解决方法:
您也可以将您的方法更改为以下方法,包括将“async”关键字添加到其签名中,如果不使用 await 会有投诉,但作为交换,您将不需要 return 价值。由于它没有 'await' 效果与同步版本有点相同,但不会抛出异常。
protected override async Task OnInitializedAsync()
{
NavigationManager.NavigateTo("Login");
}
这是我在路由中使用 RedirectToLogin.razor 组件的示例
@inject NavigationManager NavigationManager
@code{
protected override async Task OnInitializedAsync()
{
var returnUrl = "~/" + NavigationManager.ToBaseRelativePath(NavigationManager.Uri);
NavigationManager.NavigateTo($"Identity/Account/Login?returnUrl={returnUrl}", forceLoad:false);
}
}
在我的 App.razor
<Found Context="routeData">
<AuthorizeRouteView RouteData="@routeData" DefaultLayout="@typeof(MainLayout)">
<NotAuthorized>
<RedirectToLogin />
</NotAuthorized>
</AuthorizeRouteView>
</Found>
在 .NET 5.0 中,在 _host.cshtml 文件中。在“blazor_error_ui”部分之后的行中添加以下@(await Html.RenderComponentAsync(RenderMode.Server))。
使用 OnInitializedAsync 替换 OnInitialized
protected override async Task OnInitializedAsync()
{
nav.NavigateTo("/login", true);
await base.OnInitializedAsync();
}
我正在试用 Blazor ServerSide,并创建了一个组件以在用户未登录时重定向到登录页面。
@inject Microsoft.AspNetCore.Components.NavigationManager NavigationManager;
@code {
/// <inheritdoc />
protected override Task OnInitializedAsync()
{
NavigationManager.NavigateTo("Login");
return Task.CompletedTask;
}
}
但总是在调用 "NavigateTo" 时抛出以下异常:
"Microsoft.AspNetCore.Components.NavigationException: Exception of type 'Microsoft.AspNetCore.Components.NavigationException' was thrown.
at Microsoft.AspNetCore.Components.Server.Circuits.RemoteNavigationManager.NavigateToCore(String uri, Boolean forceLoad)
at Microsoft.AspNetCore.Components.NavigationManager.NavigateTo(String uri, Boolean forceLoad)
at ApplySupportTool.Blazor.Pages.RedirectToLogin.OnInitializedAsync() in C:\Users\padruttn\Documents\git\ApplySupportTool\src\ApplySupportTool.Blazor\Pages\RedirectToLogin.razor:line 8"
有趣的是,尽管存在异常,但仍进行了导航。 我也尝试使用路径“/login”调用它,但这里的行为相同。
有一个 open issue on github for this problem. See also the preceeding issue,其中提到了一个可能的解决方法:将 NavigateTo
方法放入 OnAfterRender
而不是 OnInitialized
。
旧 post,但是 - 如果您是 运行 Blazor 服务器应用程序,则此行为仅在呈现模式为 "ServerPrerendered" 时发生。通过将模式更改为 "Server" 来禁用预渲染使得异常不会首先抛出:
<app>
<component type="typeof(App)" render-mode="Server" />
</app>
我搜索了当前的 Blazor 文档和更改说明,但没有发现任何提及,以防万一它对其他人有帮助...
当我试图从另一个线程调用 navigateto 时遇到了这个问题。 render-mode = "服务器" - 解决了问题
我需要将其放入 OnInitialized
而不是 OnAfterRender
,因此不得不使用 render-mode="Server"
方法,但显然不推荐这样做。
它还在 GitHub 问题中指出,这只发生在调试中,不会发生在发布中,因此一个折衷的选择是将 _Host.cshtml
更改为包含:
<environment include="Staging,Production">
<component render-mode="ServerPrerendered" type="typeof(App)" />
</environment>
<environment include="Development">
<component render-mode="Server" type="typeof(App)" />
</environment>
我 运行 遇到同样的问题并提交 issue #28355。官方的回答是,NaviagteTo
放在OnInitialized
时,忽略异常是省事的。以下是javiercn的回答:
Yes, it is completely safe to ignore it. The debugger stops because it is configured to do so, but in this case the exception is always handled. You can turn-off breaking on this exception if it's caught on the debugger settings.
Issue #13582 处理如何防止调试器在此异常处停止。
请注意,以下可能被视为解决方法:
您也可以将您的方法更改为以下方法,包括将“async”关键字添加到其签名中,如果不使用 await 会有投诉,但作为交换,您将不需要 return 价值。由于它没有 'await' 效果与同步版本有点相同,但不会抛出异常。
protected override async Task OnInitializedAsync()
{
NavigationManager.NavigateTo("Login");
}
这是我在路由中使用 RedirectToLogin.razor 组件的示例
@inject NavigationManager NavigationManager
@code{
protected override async Task OnInitializedAsync()
{
var returnUrl = "~/" + NavigationManager.ToBaseRelativePath(NavigationManager.Uri);
NavigationManager.NavigateTo($"Identity/Account/Login?returnUrl={returnUrl}", forceLoad:false);
}
}
在我的 App.razor
<Found Context="routeData">
<AuthorizeRouteView RouteData="@routeData" DefaultLayout="@typeof(MainLayout)">
<NotAuthorized>
<RedirectToLogin />
</NotAuthorized>
</AuthorizeRouteView>
</Found>
在 .NET 5.0 中,在 _host.cshtml 文件中。在“blazor_error_ui”部分之后的行中添加以下@(await Html.RenderComponentAsync(RenderMode.Server))。
使用 OnInitializedAsync 替换 OnInitialized
protected override async Task OnInitializedAsync()
{
nav.NavigateTo("/login", true);
await base.OnInitializedAsync();
}