Skip to content
代码片段 群组 项目
未验证 提交 3757908b 编辑于 作者: Steve Sanderson's avatar Steve Sanderson 提交者: GitHub
浏览文件

Fallback for Components router (imported from Blazor PR 1534) (#4794)

上级 e768a78c
No related branches found
No related tags found
无相关合并请求
...@@ -4,7 +4,6 @@ ...@@ -4,7 +4,6 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Reflection; using System.Reflection;
using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Components.Layouts; using Microsoft.AspNetCore.Components.Layouts;
using Microsoft.AspNetCore.Components.RenderTree; using Microsoft.AspNetCore.Components.RenderTree;
using Microsoft.AspNetCore.Components.Services; using Microsoft.AspNetCore.Components.Services;
...@@ -30,6 +29,11 @@ namespace Microsoft.AspNetCore.Components.Routing ...@@ -30,6 +29,11 @@ namespace Microsoft.AspNetCore.Components.Routing
/// assemblies, for components matching the URI. /// assemblies, for components matching the URI.
/// </summary> /// </summary>
[Parameter] private Assembly AppAssembly { get; set; } [Parameter] private Assembly AppAssembly { get; set; }
/// <summary>
/// Gets or sets the type of the component that should be used as a fallback when no match is found for the requested route.
/// </summary>
[Parameter] private Type FallbackComponent { get; set; }
private RouteTable Routes { get; set; } private RouteTable Routes { get; set; }
...@@ -80,9 +84,17 @@ namespace Microsoft.AspNetCore.Components.Routing ...@@ -80,9 +84,17 @@ namespace Microsoft.AspNetCore.Components.Routing
locationPath = StringUntilAny(locationPath, _queryOrHashStartChar); locationPath = StringUntilAny(locationPath, _queryOrHashStartChar);
var context = new RouteContext(locationPath); var context = new RouteContext(locationPath);
Routes.Route(context); Routes.Route(context);
if (context.Handler == null) if (context.Handler == null)
{ {
throw new InvalidOperationException($"'{nameof(Router)}' cannot find any component with a route for '/{locationPath}'."); if (FallbackComponent != null)
{
context.Handler = FallbackComponent;
}
else
{
throw new InvalidOperationException($"'{nameof(Router)}' cannot find any component with a route for '/{locationPath}', and no fallback is defined.");
}
} }
if (!typeof(IComponent).IsAssignableFrom(context.Handler)) if (!typeof(IComponent).IsAssignableFrom(context.Handler))
......
...@@ -88,6 +88,15 @@ namespace Microsoft.AspNetCore.Components.E2ETest.Tests ...@@ -88,6 +88,15 @@ namespace Microsoft.AspNetCore.Components.E2ETest.Tests
AssertHighlightedLinks("Other", "Other with base-relative URL (matches all)"); AssertHighlightedLinks("Other", "Other with base-relative URL (matches all)");
} }
[Fact]
public void CanArriveAtFallbackPageFromBadURI()
{
SetUrlViaPushState("/Oopsie_Daisies%20%This_Aint_A_Real_Page");
var app = MountTestComponent<TestRouter>();
Assert.Equal("Oops, that component wasn't found!", app.FindElement(By.Id("test-info")).Text);
}
[Fact] [Fact]
public void CanFollowLinkToOtherPage() public void CanFollowLinkToOtherPage()
{ {
......
<div id="test-info">Oops, that component wasn't found!</div>
<Router AppAssembly=typeof(BasicTestApp.Program).Assembly /> <Router AppAssembly=typeof(BasicTestApp.Program).Assembly FallbackComponent="typeof(Error404)" />
\ No newline at end of file
0% 加载中 .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册