Skip to content

Explain MapWhen vs UseWhen in the HTTP handlers migration article - #38208

Open
pkryusuf wants to merge 2 commits into
dotnet:mainfrom
pkryusuf:mapwhen-usewhen-10520
Open

pkryusuf wants to merge 2 commits into
dotnet:mainfrom
pkryusuf:mapwhen-usewhen-10520

Conversation

@pkryusuf

@pkryusuf pkryusuf commented Nov 21, 2026 •

Copy link
Copy Markdown

Fixes #10520

Adds a note to the MapWhen section of Migrate HTTP handlers to ASP.NET Core middleware: MapWhen creates a separate branch, so middleware registered after it on the main pipeline doesn't run for matching requests, while UseWhen rejoins the main pipeline. The wording follows the explanation in the issue comments and links to Branch the middleware pipeline.

The issue pointed at migration/http-modules.md, which no longer has this section; the MapWhen guidance now lives in migration/fx-to-core/areas/http-handlers.md.


Internal previews

File Preview link
aspnetcore/migration/fx-to-core/areas/http-handlers.md Learn preview

Build report

@pkryusuf
pkryusuf requested a review from guardrex as a code owner November 21, 2026 14:21
@guardrex guardrex self-assigned this Nov 21, 2026
guardrex

This comment was marked as resolved.

@pkryusuf

Copy link
Copy Markdown
Author

Done, thanks!

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

The UseWhen explanation incorrectly says it does not create a separate branch; it should describe a branch that rejoins the main pipeline when it calls next.

1 open issue
What changed in this PR

This PR updates the HTTP handlers migration guide to explain conditional middleware behavior, addressing #10520 in the article’s current location.

Changes:

  • Explains that matching MapWhen requests don't continue through downstream middleware on the main pipeline.
  • Introduces UseWhen as the alternative for continuing through the pipeline and links to the middleware branching guide.
File Description
aspnetcore/​migration/​fx-to-core/​areas/​http-handlers.md Adds the MapWhen versus UseWhen explanation and supporting references.

Review effort: Balanced


Middleware added to the pipeline before the branch will be invoked on all requests; the branch will have no impact on them.

<xref:Microsoft.AspNetCore.Builder.MapWhenExtensions.MapWhen*> creates a separate branch. When the predicate returns `true`, only the middleware added to the branch runs, and middleware registered on the main pipeline after `MapWhen` doesn't run for that request. To run middleware conditionally and then continue with the rest of the pipeline, use <xref:Microsoft.AspNetCore.Builder.UseWhenExtensions.UseWhen*> instead. `UseWhen` doesn't create a separate branch, so middleware registered after it runs whether the predicate returns `true` or `false`, unless the conditional middleware short-circuits the request. For more information, see [Branch the middleware pipeline](../../../fundamentals/middleware/index.md#branch-the-middleware-pipeline).

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The suggested change LGTM ☝️.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

UseWhen works instead of Mapwhen

3 participants