我有一个ASP.NET Web API项目,我正在尝试使用波斯日历存储我的日期.这是我的POCO课程.
public class Person { PersianCalendar p = new PersianCalendar(); public Person() { } public Person(string firstName,string lastName,PersianCalendar birthDate) { FirstName = firstName; LastName = lastName; DateOfBirth = birthDate; } public int PersonId { get; set; } public string FirstName { get; set; } public string LastName { get; set; } public PersianCalendar DateOfBirth { get; set; } }
我为我的API创建了一个POST方法.
public HttpResponseMessage Post([FromBody] Person _p) { try { db.Persons.Add(_p); db.SaveChanges(); var msg = Request.CreateResponse(HttpStatusCode.Created,_p); msg.Headers.Location = new Uri(Request.RequestUri + _p.PersonId.ToString()); return msg; } catch(Exception ex) { return Request.CreateErrorResponse(HttpStatusCode.BadRequest,ex); } }
但在Chrome开发工具中,我的Person类的断点birthDate设置为null.
任何人都可以帮我解决错误吗?
如何以波斯语格式发送日期?
解决方法
模型POCO中的DateOfBirth不应该是PersianCalendar类型.日历用于操作DateTime对象,而不是DateTime对象的容器.
您的模型应将出生日期存储为正常的DateTime,然后您可以使用PersianCalendar来操作它.简单(但有点难看)的方法是使用DateTime字段和单独的公共字符串属性来获取和设置由波斯日历格式化的日期:
public class Person { PersianCalendar p = new PersianCalendar(); public Person() { } public Person(string firstName,DateTime birthDate) { FirstName = firstName; LastName = lastName; DateOfBirth = birthDate; } public Person(string firstName,string birthDateString) { FirstName = firstName; LastName = lastName; DateOfBirthString = birthDateString; } public int PersonId { get; set; } public string FirstName { get; set; } public string LastName { get; set; } public DateTime DateOfBirth; public string DateOfBirthString { get { return string.Format("{0}/{1}/{2}",p.GetYear(DateOfBirth),p.GetMonth(DateOfBirth),p.GetDayOfMonth(DateOfBirth)); } set { var parts = value.Split('/'); DateOfBirth = p.ToDateTime(int.Parse(parts[0]),int.Parse(parts[1]),int.Parse(parts[2]),0); } } }
请记住,DateTime代表一般特定时刻.它没有任何唯一正确的字符串表示形式.但是如果你使用上面的内容,那么你就可以决定对你的应用程序唯一正确的字符串表示是一个波斯日期,格式为yyyy / MM / dd.
编辑:您还可以创建自定义JSON JsonConverter并在模型中使用它.您的POCO模型可能如下所示:
public class Person { public Person() { } public Person(string firstName,DateTime birthDate) { FirstName = firstName; LastName = lastName; DateOfBirth = birthDate; } public int PersonId { get; set; } public string FirstName { get; set; } public string LastName { get; set; } [JsonConverter(typeof(PersianDateConverter))] public DateTime DateOfBirth; }
PersianDateConverter类看起来像这样:
public class PersianDateConverter : JsonConverter { PersianCalendar pc = new PersianCalendar(); public PersianDateConverter() { } public override object ReadJson(JsonReader reader,Type objectType,object existingValue,JsonSerializer serializer) { string persianDateString = reader.Value as string; if (persianDateString == null) throw new ArgumentException("Error in PersionDateConverter.ReadJson: Got null string"); var parts = persianDateString.Split('/'); return pc.ToDateTime(int.Parse(parts[0]),0); } public override void WriteJson(JsonWriter writer,object value,JsonSerializer serializer) { DateTime d = (DateTime)value; string output = string.Format("{0}/{1}/{2}",pc.GetYear(d),pc.GetMonth(d),pc.GetDayOfMonth(d)); writer.WriteValue(output); } public override bool CanConvert(Type objectType) { if (objectType.Name == "DateTime") return true; return false; } }
注意:此PersianDateConverter未针对边缘情况进行良好测试.只要它操作的数据有效,似乎工作正常.