-
Notifications
You must be signed in to change notification settings - Fork 712
Using v1 pattern in url for minimal API .NET 6 versioning #830
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Thanks for reporting this. There are lot of possible setup variations and I hadn't tried this one. There is a bug in ASP.NET Core's implementation of the API Explorer for Minimal APIs that is causing this. I've reported it so we'll see what they say. API Versioning relies on the In the meantime, there are a few possible workarounds. The best option at the moment would probably be to use a custom
This effectively the same steps API Versioning uses. Without a match parameter though, there's no way to accurately know how to generate the |
The ASP.NET Core team confirmed the bug, but they've yet to provide any feedback as to when the fix might be published. I'm getting the impression that it may not happen until .NET 7.0. It's a pretty glaring oversight IMHO and should be included in a service release. The bug affects more than just API Versioning. Regardless, I've come up with a way to work around the problem and hide the internal yuckiness of it. It's a sticking point for me that the fix should not result in a change (e.g. breaking change) to API Versioning's public API surface. It's not the ideal solution, but I've got something working that will make this scenario work without exposing the bits that will likely go away in a future patch or version of ASP.NET Core. Expect to see this fixed in the next release. |
@knightian is that a question? Looks like you are probably missing a using Asp.Versioning;
using Asp.Versioning.Conventions; |
Entire Asp.Versioning.Mvc.ApiExplorer was not even added to my project by nuget. Seems it needed to be manually added. Didn't think I would be using Mvc stuff for minimal? |
Yeah, it's unfortunate, but the much of the implementation for the API Explorer (Microsoft.AspNetCore.Mvc.ApiExplorer) depends on Microsoft.AspNetCore.Mvc.Core. I'm not entirely sure why that is, but it's a decision that was made years ago and can't easily change. The core abstractions - Microsoft.AspNetCore.Mvc.Abstractions - is part of the MVC Core surface area, but doesn't have a dependency on the Microsoft.AspNetCore.Mvc.Core. I considered attempting to split the two apart, but it wasn't buying much value. I did move the abstractions into There are arguably a few things under the MVC name that aren't really MVC. Term Web API is still used even though it's branded under MVC. If Swagger/OpenAPI would have been more mature - say 5+ years ago, I think you might have seen things unfold and be named differently. I hope that helps clear things up. |
There are some additional improvements for .NET 7 that should allow declaring an Thanks for reporting the issue and the discussion. If somehow this did not solve your problem, I'm happy to reopen the issue and discuss further. |
Hi, I'm getting the same error. My swagger set up is taken from the example projects. Asp.Versioning.Mvc: 6.2.1 [Route("/v{apiVersion}")]
[ApiVersion("2.0")]
public class ListCountryCodesController_v2
{
[HttpGet("country-codes", Name = Routes.ListCountryCodes_v2)]
[MapToApiVersion("2.0")]
public ActionResult<List<CountryCodeViewModel>> GetAll()
{
// implementation omitted
}
} services
.AddControllers();
services
.AddApiVersioning(options =>
{
options.ApiVersionReader = new UrlSegmentApiVersionReader();
options.DefaultApiVersion = new ApiVersion(1, 0);
options.ReportApiVersions = true;
})
.AddMvc()
.AddApiExplorer(options =>
{
options.GroupNameFormat = "'v'VVVV";
options.SubstituteApiVersionInUrl = true;
});
services.AddTransient<IApiControllerSpecification, AllApiControllerSpec>(); // treat all controllers as "API controllers" |
@jamesfoster This is happening because you are missing the route constraint. You have: [Route("/v{apiVersion}")] but it needs to be: [Route("/v{version:apiVersion}")]
You can name the route parameter whatever you want. In the example it's Also note that the name |
Thanks, that worked. Much appreciated. I realised the name of the controller was causing it not to be picked up. I'm probably going to organise the files in folders/namespaces by version. Thanks so much for your help. |
@commonsensesoftware Hi there sorry to disturb, I seem to be running into the same issue as above. |
@djrietberg I think you may be confused as to which builder.Services.AddEndpointsApiExplorer(); // ← ASP.NET Core
builder.Services
.AddApiVersioning(options => options.ApiVersionReader = new UrlSegmentApiVersionReader()) // ← API Versioning
.AddApiExplorer(options => // ← API Versioning
{
options.GroupNameFormat = "'v'VVV";
options.SubstituteApiVersionInUrl = true;
});
You always need |
You are absolutely right, thanks... small distinctions make a big difference.. as always.. Many thanks for the quick reply.. |
Hi
I want to use
/v1/orders
or/v2/orders
pattern using api versioningWell, I configured like:
I expected if type in url like
/v1/orders
that would go to firstMapGet
but I got 404 page. As well as for/v2/orders
If I type
/orders
without anyvX
then I got 400 that api version is not specified.How to solve this ?
A solution would be
app.MapGet("v{version:apiVersion}/orders")
but in SwaggerUI displays like:But is not quite good because I already selected api version from top right corner dropdown, don't see the purpose of
{version}
in callThe text was updated successfully, but these errors were encountered: