-
Notifications
You must be signed in to change notification settings - Fork 25.1k
Description
Description
Yes, @guardrex, I learnt how to open issues properly!
The snippet I added for "Support for Not Found responses in apps without Blazor's router" -> "When the response has started, the NotFoundEventArgs.Path can be used by subscribing to the OnNotFoundEvent in the router" turns out to be confusing. It implies that custom router would have RouteAttribute that is typical to the default router. My proposal of updating the snippet:
OLD:
@code {
[CascadingParameter]
public HttpContext? HttpContext { get; set; }
private void OnNotFoundEvent(object sender, NotFoundEventArgs e)
{
// Only execute the logic if HTTP response has started,
// because setting NotFoundEventArgs.Path blocks re-execution
if (HttpContext?.Response.HasStarted == false)
{
return;
}
var type = typeof(CustomNotFoundPage);
var routeAttributes = type.GetCustomAttributes<RouteAttribute>(inherit: true);
if (routeAttributes.Length == 0)
{
throw new InvalidOperationException($"The type {type.FullName} " +
$"doesn't have a {typeof(RouteAttribute).FullName} applied.");
}
var routeAttribute = (RouteAttribute)routeAttributes[0];
if (routeAttribute.Template != null)
{
e.Path = routeAttribute.Template;
}
}
}NEW
@code {
[CascadingParameter]
public HttpContext? HttpContext { get; set; }
private void OnNotFoundEvent(object sender, NotFoundEventArgs e)
{
// Only execute the logic if HTTP response has started,
// because setting NotFoundEventArgs.Path blocks re-execution
if (HttpContext?.Response.HasStarted == false)
{
return;
}
// write your own method for getting the route of not found page you want to display
string notFoundRoutePath = GetNotFoundRoutePathFromCustomRouter();
e.Path = notFoundRoutePath;
}
}What's more, I can see now that the descriptions for "Support for Not Found responses in apps without Blazor's router" is a bit misleading. I would update them as well, just to be sure:
OLD:
Apps that implement a custom router can use `NavigationManager.NotFound`. The custom router can render Not Found content from two sources, depending on the state of the response:
- Regardless of the response state, the re-execution path to the page can used by passing it to [UseStatusCodePagesWithReExecute](https://learn.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.builder.statuscodepagesextensions.usestatuscodepageswithreexecute)
- When the response has started, the [NotFoundEventArgs.Path](https://learn.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.components.routing.notfoundeventargs.path) can be used by subscribing to the OnNotFoundEvent in the router:
NEW:
Apps that implement a custom router can use `NavigationManager.NotFound`. There are two ways of informing the renderer what page should be rendered on `NavigationManager.NotFound` call.
- A recommended way, working regardless of the response state. Set [UseStatusCodePagesWithReExecute](https://learn.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.builder.statuscodepagesextensions.usestatuscodepageswithreexecute) re-execution middleware in your application. The path passed to the middleware method will be rendered on `NavigationManager.NotFound` call.
- In case you don't want to set [UseStatusCodePagesWithReExecute](https://learn.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.builder.statuscodepagesextensions.usestatuscodepageswithreexecute) , your application can still support `NavigationManager.NotFound` for responses that have already started. Subscribe to `OnNotFoundEvent` in the router and assign the not found page path to `NotFoundEventArgs.Path`, to inform renderer what content should be rendered on `NavigationManager.NotFound`.
Re-exec snippet stays without changes.
This update has a low priority, as it's only clarifying the already existing docs.
Page URL
Content source URL
https://github.com/dotnet/AspNetCore.Docs/blob/main/aspnetcore/release-notes/aspnetcore-10.0.md
Document ID
a7ad4b01-2333-32d8-b759-e044edfb9102
Platform Id
2f8fb8ab-5309-0a77-987a-c867c6517d2b
Article author
Metadata
- ID: a7ad4b01-2333-32d8-b759-e044edfb9102
- PlatformId: 2f8fb8ab-5309-0a77-987a-c867c6517d2b
- Service: aspnet-core
- Sub-service: release-notes
Metadata
Metadata
Assignees
Type
Projects
Status