Reputation: 389
I am using Devexpress XPO as ORM and I am going to create an OData Web API with ASP.NET core. Devexpress has created an example in ASP.NET classic and I have one code line (in CS/ODataService/Globals.asax.cs, Line 13) that I cannot "convert" because there seems to be no counterpart in ASP.NET core:
GlobalConfiguration.Configuration.Services.Replace(typeof(IBodyModelValidator), new CustomBodyModelValidator());
First I am puzzled by ...Services.Replace since I did not find the suitable "command" in core and (most important) in core seems to be no counterpart for IBodyModelValidator. How would I "transform" that into ASP.NET core?
Any suggestions would be appreciated!
Upvotes: 4
Views: 449
Reputation: 737
I know this is old but this is the first post I came across when looking for the answer. Here is how I solved the issue:
public class CustomBodyModelValidator : ObjectModelValidator
{
public CustomBodyModelValidator(IModelMetadataProvider modelMetadataProvider, IList<IModelValidatorProvider> validatorProviders)
: base(modelMetadataProvider, validatorProviders) { }
public override ValidationVisitor GetValidationVisitor(ActionContext actionContext, IModelValidatorProvider validatorProvider,
ValidatorCache validatorCache, IModelMetadataProvider metadataProvider, ValidationStateDictionary validationState)
{
return new(actionContext, validatorProvider, validatorCache, metadataProvider, validationState)
{
ValidateComplexTypesIfChildValidationFails = true
};
}
public override void Validate(ActionContext actionContext, ValidationStateDictionary validationState, string prefix, object model,
ModelMetadata metadata, object container)
{
//TODO: put your logic here
base.Validate(actionContext, validationState, prefix, model, metadata, container);
}
}
And then register it in the Startup.cs
:
services.AddSingleton<IObjectModelValidator>(provider =>
{
var metadataProvider = provider.GetRequiredService<IModelMetadataProvider>();
var options = provider.GetRequiredService<IOptions<MvcOptions>>().Value;
return new CustomBodyModelValidator(metadataProvider, options.ModelValidatorProviders);
});
Upvotes: 0