Description
Background
The IApiVersioningFeature
is only allocated and assigned once per request. The original expectation was that the middleware would only be entered a single time and if re-entered, it should be idempotent. When the legacy routing system (e.g. IRouter
) is used with API Versioning in ASP.NET Core, the IApiVersioningFeature.SelectionResult
becomes stateful for the entire request. If the pipeline, and hence all the middleware, runs more than once, the IApiVersioningFeature.SelectionResult
should be cleared or otherwise reset. Failure to do so can result in the incorrect action being selected at the end. This could also result in an ambiguous match result.
Enhancement
Clear any previous values in IApiVersioningFeature.SelectionResult
each time the middleware is entered.
Workaround
This problem can currently be worked around by explicitly reinitializing the IApiVersioningFeature
instance with custom middleware as follows:
app.Use((context, next) =>
{
context.Features.Set<IApiVersioningFeature>(new ApiVersioningFeature(context));
return next(context);
});
NOTE: This only applies to the legacy routing system. Endpoint Routing is unaffected by this because it does not use
IApiVersioningFeature.SelectionResult
.