-
Notifications
You must be signed in to change notification settings - Fork 714
I can't get the routing by api version to work (web api, old style asp startup class) #1107
-
I'm following the basic web api example but getting an AmbiguousMatchException when making a request.
Since the example is for the new top level asp setup and I have the older startup + program file setup I had to adapt a bit,
services.AddApiVersioning(options => { options.DefaultApiVersion = new ApiVersion(1); options.ReportApiVersions = true; options.AssumeDefaultVersionWhenUnspecified = true; options.ApiVersionReader = ApiVersionReader.Combine( new UrlSegmentApiVersionReader(), new HeaderApiVersionReader("X-Api-Version"), new QueryStringApiVersionReader()); }).AddMvc();
inside ConfigureServices.
any ideas on what I might be doing wrong?
Beta Was this translation helpful? Give feedback.
All reactions
Replies: 3 comments 2 replies
-
Spent hrs trying to solve similar issue. I used the old deprecated version and that sorted the issue. No expert but must be the new version.
Beta Was this translation helpful? Give feedback.
All reactions
-
Do you have an example or repro of a controller that produces this error?
Beta Was this translation helpful? Give feedback.
All reactions
-
@ofirgeller Much apologies! I'm just seeing this discussion and realized I've never replied. 😞 No donut for me.
The 2 most common scenarios that can produce this are:
- Forgetting
IApiVersioningBuilder.AddMvc()
- This is different from the old version because that's how controllers and Minimal APIs are separated
AddMvc
does not add the whole MVC stack and is different fromIServiceCollection.AddMvc
; only MVC (Core) is added- You have this, so that shouldn't be the problem
- Not applying
[ApiController]
- This is the required convention to signal a controller is an API controller (versus a UI controller if you have both)
- You can apply it directly on a controller
- You can also apply it to all controllers a la:
[assembly: ApiController]
- If you don't use the API Behaviors, then you need to a custom
IApiControllerSpecification
so API Versioning picks it up- I won't elaborate further until you confirm you're in that position. I don't want you to go down a 🐰 🕳️
Beta Was this translation helpful? Give feedback.
All reactions
-
Beta Was this translation helpful? Give feedback.
All reactions
-
👍 1
-
As mentioned above, I'm 99% sure this is happening in your case because you do not have a call to .AddMvc()
here. IServiceCollection.AddApiVersioning
only adds the barebones services for use with Minimal APIs. AddApiVersioning
returns a IApiVersioningBuilder
, which can be used to chain further configuration options. Under the hood, IApiVersioningBuilder.AddMvc
will call through to IServiceCollection.AddMvcCore
. Hopefully that works for you and gets you on your way.
Minimal APIs were not introduced until .NET 6. The last supported version in the old libraries were .NET 5, which is why you didn't see this problem nor have to configure it otherwise. The migration guide has more information that you may find useful.
Beta Was this translation helpful? Give feedback.