I have been trying to implement versioning in my ASP.NET Core 6.0 Web API using Microsoft.AspNetCore.Mvc.Versioning.
I want to use separate v1 and v2 folders for my versions:
Controllers
- v1
- MyController
- v2
- MyController
However, going down this path I end up with different V1.0 and V2.0 folders for everything.
I end up with two separate namespaces and module names as well.
namespace MyAPI.Models.V1
{
public class MyModelsV1
{
namespace MyAPI.Models.V2
{
public class MyModelsV2
{
A problem arises in Program.cs with dependency injection. I'll use my identity set up as an example.
builder.Services.AddDefaultIdentity<AppUser>()
.AddRoles<IdentityRole>()
.AddClaimsPrincipalFactory<AppUserClaimsPrincipalFactory>()
.AddDefaultTokenProviders()
.AddEntityFrameworkStores<AppDbContext>();
Now there is AppUserV1, AppUserV2 and AppUserClaimsPrincipalFactoryV1 and AppUserClaimsPrincipalFactoryV2.
And there will be AppDbContextV1 and AppDbContextV2 as well.
So am I using a wrong approach? Do I just build two separate services for V1 and V2?
builder.Services.AddDefaultIdentity<AppUserV1>()
...
builder.Services.AddDefaultIdentity<AppUserV2>()
...
How would I implement the v1.0 and v2.0 folder approach in program.cs?
Are there any good end to end guides for this versioning approach? Or should I be doing something else?