Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save MahdiKarimipour/cea5622d8cd65621cbdf1c392b81474e to your computer and use it in GitHub Desktop.
Save MahdiKarimipour/cea5622d8cd65621cbdf1c392b81474e to your computer and use it in GitHub Desktop.
Configures Route Versioning for Asp.NET Web APIs
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