Customize OData Options
- 2 minutes to read
You can customize OData options in the Startup.cs file. Use the services.AddControllers().AddOData method for this purpose.
Change the Expansion Depth for Related Business Objects
To specify the ODataValidationSettings.MaxExpansionDepth option, follow the steps below:
Implement a custom ODataQueryValidator service to set MaxExpansionDepth:
using Microsoft.AspNetCore.OData.Query.Validator; using Microsoft.AspNetCore.OData.Query; // ... public class MyODataQueryValidator : ODataQueryValidator { public override void Validate(ODataQueryOptions options, ODataValidationSettings validationSettings){ validationSettings.MaxExpansionDepth = 3; base.Validate(options, validationSettings); } }
Replace the default ODataQueryValidator service with the custom service in the ConfigureServices method:
File: MySolution.WebApi\Startup.cs (MySolution.Blazor.Server\Startup.cs)
public void ConfigureServices(IServiceCollection services) { // ... services.AddControllers().AddOData((options, serviceProvider) => { options .EnableQueryFeatures(100) .AddRouteComponents("api/odata", new EdmModelBuilder(serviceProvider).GetEdmModel(), odataServices => { odataServices.AddSingleton<ODataQueryValidator, MyODataQueryValidator>(); }); }); // ... }
Change an EDM Model Structure using ODataModelBuilder
To customize the OData entity model structure at runtime (change types, properties, actions, etc.), implement the DevExpress.ExpressApp.WebApi.Services.IEdmModelCustomizer interface in a custom class and manipulate ODataModelBuilder in the CustomizeEdmModel method as needed.
You can register your custom IEdmModelCustomizer implementer after the services.AddXafWebApi
call in the ConfigureServices
method:
File: MySolution.WebApi\Startup.cs (MySolution.Blazor.Server\Startup.cs)
services.AddXafWebApi(Configuration, options => { /* ... */ });
// Add a custom IEdmModelCustomizer.
services.TryAddEnumerable(ServiceDescriptor.Transient<IEdmModelCustomizer, CustomEdmModelCustomizer>());
// OR
// Remove a built-in IEdmModelCustomizer.
// var serviceDescriptor = services.Single(descriptor => descriptor.ImplementationType?.Name == "PersistentAliasEdmModelCustomizer");
// services.Remove(serviceDescriptor);
For more information, review the following:
- Model builders overview;
- Code of the built-in IEdmModelCustomizer implementers (DateTimeNullableEdmModelCustomizer, PersistentAliasEdmModelCustomizer, etc.) within the DevExpress.ExpressApp.WebApi library sources;
- Support articles: T1096425 | T1041495 | T1101266.