Getting 404 instead of 400 with versioning in routes #1049
-
I'm currently updating our .Net API from .Net 6 with Microsoft.AspNetCore.Mvc.Versioning v5.0.0 to .Net 8 with Asp.Versioning.Mvc v7.1.0. So far everything has been pretty straight forward but running into an issue with invalid versions throwing 404 errors instead of 400. With v5 this all worked as expected but after moving to the latest verison, it seems to hit routing prior to checking for bad versions. On a test, I downloaded the example projects and got the same behavior so I'm not certain if this is as intended or a bug. If I switch my API to use the query string versioning, everything works as expected. Outside of creating a custom middleware to pull routes and compare versions manually, I can't figure out a way to duplicate the behavior from the previous version. Startup.cs (API has been upgraded since from 3.1)
Controllers
|
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Unfortunately, this is a fundamental change in aligning to all of the routing behaviors. This is called out in the migration guide and was previously mentioned in the release notes. This wasn't a decision taken lightly, but there was no way around it. Not fixing the routing meant that One possible alternative would be to create custom middleware that only looks for var resolvedVersion = httpContext.GetRequestedApiVersion() is not null; Using the combination of
|
Beta Was this translation helpful? Give feedback.
Unfortunately, this is a fundamental change in aligning to all of the routing behaviors. This is called out in the migration guide and was previously mentioned in the release notes.
This wasn't a decision taken lightly, but there was no way around it. Not fixing the routing meant that
406
and415
did not work properly. Without going way down into the weeds, the reason that it no longer matches/works is because the API version is part of route resolution now. If an API version doesn't match, it's very hard, if not impossible to know if the rest of the path might match in the route tree. The routing system doesn't really have a way to skip matching a route parameter temporarily to see if th…