Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

I can't get the routing by api version to work (web api, old style asp startup class) #1107

ofirgeller started this conversation in General
Discussion options

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?

You must be logged in to vote

Replies: 3 comments · 2 replies

Comment options

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.

You must be logged in to vote
1 reply
@commonsensesoftware
Comment options

Do you have an example or repro of a controller that produces this error?

Comment options

@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:

  1. 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 from IServiceCollection.AddMvc; only MVC (Core) is added
    • You have this, so that shouldn't be the problem
  2. 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 🐰 🕳️
You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
1 reply
@commonsensesoftware
Comment options

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
3 participants
Morty Proxy This is a proxified and sanitized view of the page, visit original site.