dotnet / dotnet/aspnetcore

Enhanced nav: support redirections triggered by non-Blazor code

Open
#53,606 6 comments 4 reactions 0 assignees View on GitHub
area-blazor
Dominant language
C#
Stars
38.4k
Forks
10.9k
Avg merge
2d 10h
Merged PRs (30d)
281

Description

Currently, during an enhanced nav POST request, redirections to external URLs will only work if they are triggered by `NavigationManager.NavigateTo(url)`. They do **not** work if triggered through any other API, such as `httpContext.Response.Redirect`. This is a problem because other ASP.NET middleware may issue redirections, for example to auth endpoints.

### Customer impact

Blazor enhanced forms are often used to perform data access, such as saving changes via a POST, or simpler gestures like "add to cart" in eShop. But it's always possible that your authentication token has expired and that some non-Blazor middleware is going to intercept the request and perform a redirection to an external URL for reauthentication.

Right now there's no sensible way to manage this other than simply not enabling enhancement on the form. Unless you can 100% guarantee that no other middleware might issue a redirection, you just can't use `Enhance` or `data-enhance`, because any such redirection will lead to an error.

We could make redirections work regardless of what code triggered them.

### Proposed solution

Blazor should, in some way, hook into the ASP.NET Core request processing pipeline (via middleware or any other mechanism) so that it can detect the case of "enhanced nav request leads to a redirection" and perform the same response translation that Blazor's renderer does for `NavigationException`.

The logic would look the same as we have in EndpointHtmlRenderer.Prerendering.cs: https://github.com/dotnet/aspnetcore/blob/5d0e3b0513a1fd71aa4a63029b91d57504b59345/src/Components/Endpoints/src/Rendering/EndpointHtmlRenderer.Prerendering.cs#L203-L213

The perf impact would be negligible since it starts by checking for method=post and the presence of the enhanced nav marker header, and if either is absent it's a no-op.

### Note

We considered doing this in .NET 8 but decided not to take the risk of affecting non-Blazor endpoints so close to the final release. For .NET 9 we have more opportunity to attempt it.

Contributor guide

Open the contributing guide

Research direction

Start by reading EndpointHtmlRenderer.Prerendering.cs around lines 203-213 and tracing how enhanced POST requests are marked in the ASP.NET Core request pipeline. Determine where redirects from non-Blazor middleware can be detected and translated for enhanced navigation, while confirming ordinary non-Blazor endpoints remain unaffected.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp
Domain
backend
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.