Created
June 24, 2021 22:28
-
-
Save MahdiKarimipour/cea5622d8cd65621cbdf1c392b81474e to your computer and use it in GitHub Desktop.
Configures Route Versioning for Asp.NET Web APIs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private void ConfigureVersioning(IServiceCollection services) | |
{ | |
services.AddControllersWithViews(o => | |
{ | |
o.UseGeneralRoutePrefix("/v{version:apiVersion}"); | |
}).AddNewtonsoftJson(options => | |
{ | |
options.SerializerSettings.MaxDepth = 1; | |
options.SerializerSettings.NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore; | |
options.SerializerSettings.DefaultValueHandling = Newtonsoft.Json.DefaultValueHandling.Ignore; | |
}); | |
services.AddApiVersioning(o => o.ReportApiVersions = true); | |
services.AddVersionedApiExplorer( | |
options => | |
{ | |
// add the versioned api explorer, which also adds IApiVersionDescriptionProvider service | |
// note: the specified format code will format the version as "'v'major[.minor][-status]" | |
options.GroupNameFormat = "'v'VVV"; | |
// note: this option is only necessary when versioning by url segment. the SubstitutionFormat | |
// can also be used to control the format of the API version in route templates | |
options.SubstituteApiVersionInUrl = true; | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment