0

I have a ODataPayloadValueConverter and I am trying to add it to my configuration

public class DateTimeValueLocaliser : ODataPayloadValueConverter {
    public override object ConvertToPayloadValue(object value, IEdmTypeReference edmTypeReference)
    {
        if (value is DateTime)
        {
            return new DateTime();
        }
        else
        {
            return base.ConvertToPayloadValue(value, edmTypeReference);
        }
    } }

My Startup.cs

    services.AddControllers(options =>
        {
            options.EnableEndpointRouting = false;
            var policy = new AuthorizationPolicyBuilder()
                .RequireAuthenticatedUser()
                .Build();
            options.Filters.Add(new AuthorizeFilter(policy));
        })
        .SetCompatibilityVersion(CompatibilityVersion.Latest)
        .AddNewtonsoftJson(opt =>
        {
            opt.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
            opt.SerializerSettings.ContractResolver = new DefaultContractResolver();
            opt.SerializerSettings.Formatting = Formatting.Indented;
        }).AddOData(options =>
        {
            options.AddRouteComponents("odata", new MyODataModelBuilder().GetEdmModel());
        });
    services.AddMvcCore();

How and where should I add to the configuration the payload value converter. Can I add it to the ModelBuilder IEdmModel function?

Serge Zoghbi
  • 13
  • 1
  • 2

1 Answers1

0

Since .NET 5 this has changed, you can now set it in the AddRouteComponents

.AddOData(options =>
        {
            options.AddRouteComponents("odata", GetEdmModel(), action =>
            {
                action.AddSingleton(typeof(ODataPayloadValueConverter), new ODataByteArrayAsHexJsonConverter());
            })
            .Filter().Select().Expand().Count().OrderBy().SetMaxTop(50).SkipToken();
        });

Check the following article too .NET Core Web API with ODATA serializing DateTime

Andrey Belykh
  • 2,578
  • 4
  • 32
  • 46
jmahlitz
  • 41
  • 2