version 1.0 controller:
using Microsoft.AspNetCore.Mvc;
namespace Web.API.VersioningTest1.Controllers
{
[ApiVersion("1.0")]
[Route("monkeys")]
public class MonkeysController : ControllerBase
{
[HttpGet]
public IActionResult Get()
{
var monkey = new { Id = 1, Name = "Forest Monkey" , version = "1.0" };
return Ok(monkey);
}
}
}
Version 2.0 Controller
using Microsoft.AspNetCore.Mvc;
namespace Web.API.VersioningTest1.Controllers
{
[ApiVersion("2.0")]
[Route("monkeys")]
public class MonkeysV2Controller : ControllerBase
{
[HttpGet]
public IActionResult Get()
{
var monkey = new { Id = 1, Name = "Forest Monkey", version = "2.0" };
return Ok(monkey);
}
}
}
version configuration in Startup.cs
services.AddApiVersioning(options => {
options.AssumeDefaultVersionWhenUnspecified = true;
options.ReportApiVersions = true;
});
my test url's:
https://localhost:44306/monkeys?api-version=1.0
https://localhost:44306/monkeys?api-version=2.0
Results:
AmbiguousMatchException: The request matched multiple endpoints. Matches: Web.API.VersioningTest1.Controllers.MonkeysController.Get (Web.API.VersioningTest1) Web.API.VersioningTest1.Controllers.MonkeysV2Controller.Get2 (Web.API.VersioningTest1)
This is because your controllers do not have [ApiController] applied to them. A controller is a controller in ASP.NET Core, which means there isn't a clear way to disambiguate between a UI controller and an API controller controller. Furthermore, there are no requirements on base class (e.g. ControllerBase). Some people mix UI controllers and API controllers in the same project, but don't want to enforce versioning semantics on UI controllers (for good reason). ASP.NET Core introduced the concept of _API Conventions_ and behaviors back in 2.x. API Versioning uses this information to apply a convention for versioning. If a controller does not include [ApiController] it will be excluded from versioning by default. This behavior can be changed in a couple of ways. You can set options.UseApiBehavior = false (which has defaulted to true since 3.0) or you can replace the default filtering conventions with your own.
Adding [ApiController] fixed the versioning issue
Thanks
Most helpful comment
This is because your controllers do not have
[ApiController]applied to them. A controller is a controller in ASP.NET Core, which means there isn't a clear way to disambiguate between a UI controller and an API controller controller. Furthermore, there are no requirements on base class (e.g. ControllerBase). Some people mix UI controllers and API controllers in the same project, but don't want to enforce versioning semantics on UI controllers (for good reason). ASP.NET Core introduced the concept of _API Conventions_ and behaviors back in 2.x. API Versioning uses this information to apply a convention for versioning. If a controller does not include[ApiController]it will be excluded from versioning by default. This behavior can be changed in a couple of ways. You can setoptions.UseApiBehavior = false(which has defaulted totruesince 3.0) or you can replace the default filtering conventions with your own.