Skip to content

Commit 212c260

Browse files
committed
refactoring
1 parent 5bb9325 commit 212c260

File tree

1 file changed

+14
-40
lines changed

1 file changed

+14
-40
lines changed

src/OData.WebApi/Startup.cs

Lines changed: 14 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -28,44 +28,18 @@ public Startup(IConfiguration configuration)
2828

2929
public void ConfigureServices(IServiceCollection services)
3030
{
31-
services.AddDbContext<ProductDbContext>(options =>
32-
options.UseNpgsql(Configuration.GetConnectionString("ProductConStr")));
33-
31+
services.AddDbContext<ProductDbContext>(
32+
options => options.UseNpgsql(Configuration.GetConnectionString("ProductConStr")));
3433
services.AddScoped(typeof(IRepository<>), typeof(Repository<>));
3534

36-
services.AddOData();
37-
services.AddMvc(options =>
38-
{
39-
options.EnableEndpointRouting = false;
40-
// https://github.com/OData/WebApi/issues/1177
41-
foreach (var outputFormatter in options.OutputFormatters.OfType<ODataOutputFormatter>()
42-
.Where(_ => _.SupportedMediaTypes.Count == 0))
43-
{
44-
outputFormatter.SupportedMediaTypes.Add(
45-
new MediaTypeHeaderValue("application/prs.odatatestxx-odata"));
46-
}
47-
48-
foreach (var inputFormatter in options.InputFormatters.OfType<ODataInputFormatter>()
49-
.Where(_ => _.SupportedMediaTypes.Count == 0))
50-
{
51-
inputFormatter.SupportedMediaTypes.Add(
52-
new MediaTypeHeaderValue("application/prs.odatatestxx-odata"));
53-
}
54-
})
35+
services
36+
.AddMvc(options => options.EnableEndpointRouting = false)
5537
.SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
38+
services.AddOData();
5639
}
5740

5841
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
5942
{
60-
if (env.IsDevelopment())
61-
{
62-
app.UseDeveloperExceptionPage();
63-
}
64-
else
65-
{
66-
app.UseHsts();
67-
}
68-
6943
using (var serviceScope = app.ApplicationServices.GetRequiredService<IServiceScopeFactory>().CreateScope())
7044
{
7145
using (var context = serviceScope.ServiceProvider.GetService<ProductDbContext>())
@@ -75,10 +49,12 @@ public void Configure(IApplicationBuilder app, IHostingEnvironment env)
7549
}
7650

7751
app.UseMvc(
78-
rb =>
52+
routeBuilder =>
7953
{
80-
rb.MapODataServiceRoute("ODataRoute", "odata", GetEdmModel());
81-
rb.EnableDependencyInjection();
54+
// Workaround: https://github.com/OData/WebApi/issues/1175
55+
// routeBuilder.EnableDependencyInjection();
56+
routeBuilder.Filter().Count().Expand().OrderBy().Select().MaxTop(1);
57+
routeBuilder.MapODataServiceRoute("ODataRoute", "odata", GetEdmModel());
8258
});
8359
}
8460

@@ -87,12 +63,10 @@ private static IEdmModel GetEdmModel()
8763
var builder = new ODataConventionModelBuilder();
8864
builder.EnableLowerCamelCase();
8965

90-
builder.EntitySet<Product>("products")
91-
.EntityType.Filter().Count().Expand().OrderBy().Page().Select();
92-
93-
builder.EntitySet<ProductCategory>("product_categories")
94-
.EntityType.Filter().Count().Expand().OrderBy().Page().Select();
95-
66+
builder.EntitySet<Product>("products");
67+
// .EntityType.Filter().Count().Expand().OrderBy().Page().Select();
68+
builder.EntitySet<ProductCategory>("product_categories");
69+
// .EntityType.Filter().Count().Expand().OrderBy().Page().Select();
9670
return builder.GetEdmModel();
9771
}
9872
}

0 commit comments

Comments
 (0)