-
Notifications
You must be signed in to change notification settings - Fork 10.3k
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
Support using Blazor pages with status code pages middleware #51203
Comments
Thanks for contacting us. We're moving this issue to the |
We've moved this issue to the Backlog milestone. This means that it is not going to be worked on for the coming release. We will reassess the backlog following the current release and consider this item at that time. To learn more about our issue management process and to have better expectation regarding different types of issues you can read our Triage Process. |
Page with plain text "Not Found" is the default implementation of the Router's NotFound razor component. That means that |
I have just encountered the same problem. Trying to get some kind of workaround but adding |
I see two possible workarounds:
<Router AppAssembly="typeof(Program).Assembly">
<Found Context="routeData">
<AuthorizeRouteView RouteData="routeData" DefaultLayout="typeof(Layout.MainLayout)">
<Authorizing>
Authorizing...
</Authorizing>
<NotAuthorized>
<RedirectToLogin></RedirectToLogin>
</NotAuthorized>
</AuthorizeRouteView>
</Found>
<NotFound>
<LayoutView Layout="typeof(Layout.MainLayout)">
<p>Route was not found</p>
</LayoutView>
</NotFound>
</Router> You would then think that the |
@mkArtakMSFT How can this not be a bug? |
@janrhansen @mkArtakMSFT I agree that this must be a bug! The documentation states that 'Blazor Web Apps don't use the NotFound parameter...', however, when setting Trying to get a genuine 404 page to render with Blazor is a total nightmare, and the documented approach of using Whilst, there is never going to be a way to get a 404 status when working interactively, it is imperative that we can generate a proper status code response from the initial server render and that we can do this without a change in URL. A 302 redirect when it should be a 404 is simply unacceptable. Take my scenario, a blog. The URL structure is simple: In order to get a 404 during the server render, I have some middleware that checks to see if the category and post exists. If not, it sets the status code on the response to 404. The problem then is that it still matches a route (I haven't found a way to stop this), so once the error page is rendered it then re-renders the matching route. By ensuring that a similar check is made within my page model, the client then renders the same. Now, say I have a situation where I need an '410 - Gone' status page, or would like to use '418 - I'm a Tea Pot' status for some reason... I'm now in the situation where the server does it's job and I can see the page (briefly), but the Router still decides to carry on and as it can't find that route, and it generates a 404 page but the status has been reported as 410 or 418. This is less of a problem, because I can catch these and they'll be re-rendered, but it means that every-time something like this is done it's run the code on the server, and then re-run the code on the server (as I'm using InteractiveServer). My feeling is that once the status code has been set to an error response, then the Router shouldn't be dealing with the route. It should be handed off to the error page(s) to then render and deal with any interaction. |
Hello @danroth27 A fix for this (specifically 404 not found) is by adding it to Routes.razor like this; @using BlazorApp21.Components.Layout
@using BlazorApp21.Components.Pages
<Router AppAssembly="typeof(Program).Assembly">
<Found Context="routeData">
<RouteView RouteData="routeData" DefaultLayout="typeof(MainLayout)" />
<FocusOnNavigate RouteData="routeData" Selector="h1" />
</Found>
<NotFound>
<MainLayout>
<Body>
<StatusCode Status="404" />
</Body>
</MainLayout>
</NotFound>
</Router> Another option which might be even more robust when it comes to all status codes, is by Excluding it from interactivity: @page "/StatusCode/{status:int}"
@attribute [ExcludeFromInteractiveRouting]
<PageTitle>@Status</PageTitle>
<h1>@Status</h1>
@code {
[Parameter]
public int Status { get; set; }
}
...
<HeadOutlet @rendermode="PageRenderMode" />
...
<Routes @rendermode="PageRenderMode" />
...
@code {
[CascadingParameter]
private HttpContext HttpContext { get; set; } = default!;
private IComponentRenderMode? PageRenderMode => HttpContext.AcceptsInteractiveRouting() ? InteractiveServer : null;
} |
If you try to use Blazor pages with the status code pages middleware you get unexpected behavior.
Repro steps:
app.UseStatusCodePagesWithReExecute("/StatusCode/{0}");
Expected result: Blazor status page renders for the 404 response.
Actual result: Blazor status page shows up briefly and then is replaced with a plain text "Not found" page.
We special case re-executed requests from the error handling middleware. We might need to do this for the status code page middleware as well.
Related issue for handling not found requests: #45654
The text was updated successfully, but these errors were encountered: