aspnet-api-versioning icon indicating copy to clipboard operation
aspnet-api-versioning copied to clipboard

URL Versioning not working as expected in new ASP.Versioning.MVC package

Open luismejia-iq opened this issue 3 years ago • 5 comments
trafficstars

Is there an existing issue for this?

  • [X] I have searched the existing issues

Describe the bug

URL versioning is giving me back the following error stating that multiple endpoints match the request.

This is happening when trying to use the new Nuget Package Asp.Versioning.Mvc. Same code works fine when using the previous Microsoft.AspNetCore.Mvc.Versioning

Sample repo: https://github.com/mejialuis28/api-versioning-error

Microsoft.AspNetCore.Routing.Matching.AmbiguousMatchException: The request matched multiple endpoints. Matches: 


ApiVersioning.Controllers.Data2Controller.Get (ApiVersioning)
ApiVersioning.Controllers.DataController.Get (ApiVersioning)
   at Microsoft.AspNetCore.Routing.Matching.DefaultEndpointSelector.ReportAmbiguity(CandidateState[] candidateState)
   at Microsoft.AspNetCore.Routing.Matching.DefaultEndpointSelector.ProcessFinalCandidates(HttpContext httpContext, CandidateState[] candidateState)
   at Microsoft.AspNetCore.Routing.Matching.DefaultEndpointSelector.Select(HttpContext httpContext, CandidateState[] candidateState)
   at Microsoft.AspNetCore.Routing.Matching.DfaMatcher.MatchAsync(HttpContext httpContext)
   at Microsoft.AspNetCore.Routing.Matching.DataSourceDependentMatcher.MatchAsync(HttpContext httpContext)
   at Microsoft.AspNetCore.Routing.EndpointRoutingMiddleware.Invoke(HttpContext httpContext)
   at Microsoft.AspNetCore.StaticFiles.StaticFileMiddleware.Invoke(HttpContext context)
   at Microsoft.AspNetCore.HttpsPolicy.HttpsRedirectionMiddleware.Invoke(HttpContext context)
   at Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.Invoke(HttpContext context)

image

Expected Behavior

Should properly route the request to the right controller action associated to the given API version.

Steps To Reproduce

Here's a sample git repo that has the issue https://github.com/mejialuis28/api-versioning-error

Run the app and try a get request to:

https://localhost:5001/v1/data/ https://localhost:5001/v2/data/

Exceptions (if any)

No response

.NET Version

3.1

Anything else?

No response

luismejia-iq avatar Sep 06 '22 20:09 luismejia-iq

I'm pretty sure you need to call AddMvc after AddApiVersioning.

I ran into a related (but not the same) issue until I noticed I was missing that line when reviewing the examples.

pinkfloydx33 avatar Sep 07 '22 00:09 pinkfloydx33

@pinkfloydx33 is correct. thank you.

The setup when using controllers should look like:

public void ConfigureServices(IServiceCollection services)
{
    services.AddControllers();
    services.AddApiVersioning(options => options.ReportApiVersions = true)
            .AddMvc();
}

This is now required because Minimal APIs do not require any part of MVC. By calling AddMvc, it adds the fundamental bits from MVC (Core), which is needed when using controllers. The repro (thanks BTW) works as expected with this setup.

commonsensesoftware avatar Sep 07 '22 02:09 commonsensesoftware

It's a bit hard to spot in the examples and I didn't see mention of it in the Readme, wiki nor the Net6 roadmap posting. Perhaps it needs a bit better documentation.

pinkfloydx33 avatar Sep 07 '22 08:09 pinkfloydx33

Thanks both @pinkfloydx33 and @commonsensesoftware for the quick answer. That indeed was the issue.

I didn't spot this in the URL versioning documentation so might be worth updating that piece for the new version.

luismejia-iq avatar Sep 07 '22 14:09 luismejia-iq

Glad you were able to get it working. Updating the wiki is on my list of things to do. Truthfully, I really want to overhaul the entire documentation site. I'm reaching the limit of what is possible with the wiki.

These changes were called out in the original 6.0 Roadmap discussion and they're reiterated in the breaking changes in the 6.0.0-preview.1 release. The most up-to-date documentation is the example projects themselves, which are all current.

commonsensesoftware avatar Sep 07 '22 15:09 commonsensesoftware

It appears that this question has been answered and resolved so I'm going to close it out. Updating the documentation is still on my to do list, but thanks for the patience.

commonsensesoftware avatar Sep 29 '22 14:09 commonsensesoftware

I managed to resolve the issue by adding .AddMvc() to AddApiVersioning() as in bellow.

builder.Services.AddApiVersioning(options => { options.ReportApiVersions = true; options.DefaultApiVersion = new ApiVersion(1, 0); options.AssumeDefaultVersionWhenUnspecified = true; }).AddMvc();

jayasankai avatar Feb 18 '24 23:02 jayasankai