Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Add Scope Awareness to Middleware and Handlers
This PR introduces scope awareness to middleware and handlers, allowing them to behave differently depending on the context in which they are executed. For instance, consider a middleware that handles path inconsistencies like
/foo////bar
, which cleans up the path and redirects the client to the correct path if it exists. Such middleware would be useful only in theNoRoute
orNoMethod
handlers, where requests that don't directly match any route are processed.Currently, there's a risk that users might mistakenly apply this middleware to all routes, when it should be limited to certain scopes, like:
Example:
The following example demonstrates how to restrict middleware execution to specific scopes:
Without scope-awareness, the middleware could mistakenly be applied to all routes, causing unnecessary redirection behavior in cases where it's not needed.
Solution:
With access to the scope inside the handler (
Context.Scope
), middleware can prevent misapplication by checking the scope and logging an error if it's executed in an unintended context:Benefits:
In addition to preventing misapplication, this feature allows middleware like OpenTelemetry to adjust behavior based on context, such as customizing the span name in traces depending on whether the handler is being executed for a route,
NoRoute
, orNoMethod
handler.