Skip to content

Commit 8d739c5

Browse files
Extracted a few more extensions
1 parent 3974dd5 commit 8d739c5

File tree

3 files changed

+54
-29
lines changed

3 files changed

+54
-29
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
using Microsoft.Extensions.DependencyInjection;
2+
3+
namespace SampleWebApiAspNetCore.Helpers
4+
{
5+
public static class CorsExtension
6+
{
7+
public static void AddCustomCors(this IServiceCollection services, string policyName)
8+
{
9+
services.AddCors(options =>
10+
{
11+
options.AddPolicy(policyName,
12+
builder =>
13+
{
14+
builder
15+
.AllowAnyOrigin()
16+
.AllowAnyHeader()
17+
.AllowAnyMethod();
18+
});
19+
});
20+
}
21+
}
22+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
using Microsoft.AspNetCore.Mvc;
2+
using Microsoft.AspNetCore.Mvc.Versioning;
3+
using Microsoft.Extensions.DependencyInjection;
4+
5+
namespace SampleWebApiAspNetCore.Helpers
6+
{
7+
public static class VersioningExtension
8+
{
9+
public static void AddVersioning(this IServiceCollection services)
10+
{
11+
services.AddApiVersioning(
12+
config =>
13+
{
14+
config.ReportApiVersions = true;
15+
config.AssumeDefaultVersionWhenUnspecified = true;
16+
config.DefaultApiVersion = new ApiVersion(1, 0);
17+
config.ApiVersionReader = new HeaderApiVersionReader("api-version");
18+
});
19+
services.AddVersionedApiExplorer(
20+
options =>
21+
{
22+
options.GroupNameFormat = "'v'VVV";
23+
24+
// note: this option is only necessary when versioning by url segment. the SubstitutionFormat
25+
// can also be used to control the format of the API version in route templates
26+
options.SubstituteApiVersionInUrl = true;
27+
});
28+
}
29+
}
30+
}

SampleWebApiAspNetCore/Startup.cs

Lines changed: 2 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -37,17 +37,7 @@ public void ConfigureServices(IServiceCollection services)
3737
{
3838
services.AddOptions();
3939
services.AddDbContext<FoodDbContext>(opt => opt.UseInMemoryDatabase("FoodDatabase"));
40-
services.AddCors(options =>
41-
{
42-
options.AddPolicy("AllowAllOrigins",
43-
builder =>
44-
{
45-
builder
46-
.AllowAnyOrigin()
47-
.AllowAnyHeader()
48-
.AllowAnyMethod();
49-
});
50-
});
40+
services.AddCustomCors("AllowAllOrigins");
5141

5242
services.AddSingleton<ISeedDataService, SeedDataService>();
5343
services.AddScoped<IFoodRepository, FoodSqlRepository>();
@@ -59,30 +49,13 @@ public void ConfigureServices(IServiceCollection services)
5949
var factory = x.GetRequiredService<IUrlHelperFactory>();
6050
return factory.GetUrlHelper(actionContext);
6151
});
62-
6352

6453
services.AddControllers()
6554
.AddNewtonsoftJson(options =>
6655
options.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver())
6756
.SetCompatibilityVersion(CompatibilityVersion.Version_3_0);
6857

69-
services.AddApiVersioning(
70-
config =>
71-
{
72-
config.ReportApiVersions = true;
73-
config.AssumeDefaultVersionWhenUnspecified = true;
74-
config.DefaultApiVersion = new ApiVersion(1, 0);
75-
config.ApiVersionReader = new HeaderApiVersionReader("api-version");
76-
});
77-
services.AddVersionedApiExplorer(
78-
options =>
79-
{
80-
options.GroupNameFormat = "'v'VVV";
81-
82-
// note: this option is only necessary when versioning by url segment. the SubstitutionFormat
83-
// can also be used to control the format of the API version in route templates
84-
options.SubstituteApiVersionInUrl = true;
85-
});
58+
services.AddVersioning();
8659
services.AddTransient<IConfigureOptions<SwaggerGenOptions>, ConfigureSwaggerOptions>();
8760
services.AddSwaggerGen();
8861

0 commit comments

Comments
 (0)