Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
450 views
in Technique[技术] by (71.8m points)

c# - 在Web API中为绑定日期属性指定日期格式(Specifying a date format for binding date property in a model in web api)

I have a class in my api that contains an id and DateTime property.

(我的api中有一个包含id和DateTime属性的类。)

public class MyTestClass
    {
        public int id { get; set; }
        public DateTime dateTime { get; set; }

    }

I was wondering if there is a way to accept only a JSON string that has a specific date format.

(我想知道是否有一种方法可以只接受具有特定日期格式的JSON字符串。)

My problem is that the model binder will parse both "21-01-1991" and "01-21-1991".

(我的问题是模型联编程序将同时解析“ 21-01-1991”和“ 01-21-1991”。)

sample API controller

(示例API控制器)

public void Post(MyTestClass myTest)
        {
            DateTime x = myTest.dateTime;
        }

I want the api to return bad request in case the user sent the dateTime property in any format other than the iso 8601 format.

(如果用户以iso 8601以外的任何格式发送dateTime属性,我希望api返回错误的请求。)

  ask by user2386390 translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...