Posting models with NodaTime auto-props to WebAPI

前端 未结 1 1733
闹比i
闹比i 2021-01-19 19:57

We are in the process of using NodaTime for time / date models in our project. The project is a web application, using WebAPI. When attempting to post a model containing Loc

相关标签:
1条回答
  • It appears to be a WebAPI issue, but I don't fully understand the root cause, nor do I particularly like this solution. However, based on the code in this answer, I came up with a workaround:

    public class CustomBodyModelValidator : DefaultBodyModelValidator
    {
        public override bool ShouldValidateType(Type type)
        {
            return type.Namespace != "NodaTime" && base.ShouldValidateType(type);
        }
    }
    

    In your configuration lambda (or wherever you are accessing the global WebAPI configuration:

    x.Services.Replace(typeof(IBodyModelValidator), new CustomBodyModelValidator());
    

    Consider this a hack of a workaround until I (or someone else) can figure out a better solution. Thanks.

    Update: On further research, it appears this has already been posted to the Noda Time issue tracker as issue #249, and a similar solution was proposed.

    0 讨论(0)
提交回复
热议问题