Skip to content

Add validation to Router component to prevent both NotFound and NotFoundPage being set #62625

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
Jul 17, 2025
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Components/Components/src/PublicAPI.Unshipped.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#nullable enable
*REMOVED*Microsoft.AspNetCore.Components.ResourceAsset.ResourceAsset(string! url, System.Collections.Generic.IReadOnlyList<Microsoft.AspNetCore.Components.ResourceAssetProperty!>? properties) -> void
Microsoft.AspNetCore.Components.ResourceAsset.ResourceAsset(string! url, System.Collections.Generic.IReadOnlyList<Microsoft.AspNetCore.Components.ResourceAssetProperty!>? properties = null) -> void
Microsoft.AspNetCore.Components.Routing.Router.NotFoundPage.get -> System.Type!
Microsoft.AspNetCore.Components.Routing.Router.NotFoundPage.get -> System.Type?
Microsoft.AspNetCore.Components.Routing.Router.NotFoundPage.set -> void
Microsoft.AspNetCore.Components.Infrastructure.ComponentsMetricsServiceCollectionExtensions
Microsoft.AspNetCore.Components.NavigationManager.OnNotFound -> System.EventHandler<Microsoft.AspNetCore.Components.Routing.NotFoundEventArgs!>!
Expand Down
16 changes: 15 additions & 1 deletion src/Components/Components/src/Routing/Router.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,14 +70,15 @@ static readonly IReadOnlyDictionary<string, object> _emptyParametersDictionary
/// Gets or sets the content to display when no match is found for the requested route.
/// </summary>
[Parameter]
[Obsolete("NotFound is deprecated. Use NotFoundPage instead.")]
public RenderFragment NotFound { get; set; }

/// <summary>
/// Gets or sets the page content to display when no match is found for the requested route.
/// </summary>
[Parameter]
[DynamicallyAccessedMembers(LinkerFlags.Component)]
public Type NotFoundPage { get; set; } = default!;
public Type? NotFoundPage { get; set; }

/// <summary>
/// Gets or sets the content to display when a match is found for the requested route.
Expand Down Expand Up @@ -143,6 +144,12 @@ public async Task SetParametersAsync(ParameterView parameters)

if (NotFoundPage != null)
{
#pragma warning disable CS0618 // Type or member is obsolete
if (NotFound != null)
{
Log.BothNotFoundParametersSet(_logger);
}
#pragma warning restore CS0618 // Type or member is obsolete
if (!typeof(IComponent).IsAssignableFrom(NotFoundPage))
{
throw new InvalidOperationException($"The type {NotFoundPage.FullName} " +
Expand Down Expand Up @@ -401,10 +408,12 @@ private void RenderNotFound()
new RouteData(NotFoundPage, _emptyParametersDictionary));
builder.CloseComponent();
}
#pragma warning disable CS0618 // Type or member is obsolete
else if (NotFound != null)
{
NotFound(builder);
}
#pragma warning restore CS0618 // Type or member is obsolete
else
{
DefaultNotFoundContent(builder);
Expand All @@ -429,6 +438,7 @@ async Task IHandleAfterRender.OnAfterRenderAsync()

private static partial class Log
{
#pragma warning disable CS0618 // Type or member is obsolete
[LoggerMessage(1, LogLevel.Debug, $"Displaying {nameof(NotFound)} because path '{{Path}}' with base URI '{{BaseUri}}' does not match any component route", EventName = "DisplayingNotFound")]
internal static partial void DisplayingNotFound(ILogger logger, string path, string baseUri);

Expand All @@ -440,5 +450,9 @@ private static partial class Log

[LoggerMessage(4, LogLevel.Debug, $"Displaying {nameof(NotFound)} on request", EventName = "DisplayingNotFoundOnRequest")]
internal static partial void DisplayingNotFound(ILogger logger);
#pragma warning restore CS0618 // Type or member is obsolete

[LoggerMessage(5, LogLevel.Warning, "Both NotFound and NotFoundPage parameters are set on Router component. NotFoundPage is preferred and NotFound will be deprecated. Consider using only NotFoundPage.", EventName = "BothNotFoundParametersSet")]
internal static partial void BothNotFoundParametersSet(ILogger logger);
}
}
Loading