I have an ASP.NET Web API project in which i'm trying to store my date using Persian calendar. Here is my POCO class.
我有一个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; }
}
I created a POST method for my API.
我为我的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);
}
}
and i'm send my POST request using Fiddler.
我正在使用Fiddler发送我的POST请求。
but in the Chrome Dev Tools, break point birthDate of my Person class is set to null. Can anyone help me with what is wrong? How can i send my date in persian format??
但在Chrome开发工具中,我的Person类的断点birthDate设置为null。任何人都可以帮我解决错误吗?如何以波斯语格式发送日期?
1 个解决方案
#1
4
The DateOfBirth
in your model POCO should not be of type PersianCalendar
. A calendar is used to manipulate DateTime
objects, not as a container of DateTime
objects.
模型POCO中的DateOfBirth不应该是PersianCalendar类型。日历用于操作DateTime对象,而不是DateTime对象的容器。
Your model should store the date of birth as a normal DateTime
, then you can use a PersianCalendar
to manipulate it. The easy (but a bit ugly) way to do this is to have a DateTime
field and a separate public string
property for getting and setting the date as formatted by the Persian calendar:
您的模型应将出生日期存储为正常的DateTime,然后您可以使用PersianCalendar来操作它。简单(但有点难看)的方法是使用DateTime字段和单独的公共字符串属性来获取和设置由波斯日历格式化的日期:
public class Person
{
PersianCalendar p = new PersianCalendar();
public Person()
{
}
public Person(string firstName, string lastName, DateTime birthDate)
{
FirstName = firstName;
LastName = lastName;
DateOfBirth = birthDate;
}
public Person(string firstName, string lastName, 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, 0, 0, 0);
}
}
}
Keep in mind that a DateTime
represents a general specific moment in time. It does not have any uniquely correct string representation. But if you use something like what I have above, then you are kind of deciding that for your application the only correct string representation is a Persian date, formatted as yyyy/MM/dd
.
请记住,DateTime代表一般特定时刻。它没有任何唯一正确的字符串表示形式。但是如果你使用上面的内容,那么你就可以决定对你的应用程序唯一正确的字符串表示是一个波斯日期,格式为yyyy / MM / dd。
EDIT: You can also create a custom JSON JsonConverter
and use that in your model. Your POCO model could then look like this:
编辑:您还可以创建自定义JSON JsonConverter并在模型中使用它。您的POCO模型可能如下所示:
public class Person
{
public Person() { }
public Person(string firstName, string lastName, 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;
}
And the PersianDateConverter
class looks like this:
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]), int.Parse(parts[1]), int.Parse(parts[2]), 0, 0, 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;
}
}
Note: This PersianDateConverter
is not well tested for edge cases. Seems to work ok as long as the data it is manipulating is valid though.
注意:此PersianDateConverter未针对边缘情况进行良好测试。只要它操作的数据有效,似乎工作正常。
#1
4
The DateOfBirth
in your model POCO should not be of type PersianCalendar
. A calendar is used to manipulate DateTime
objects, not as a container of DateTime
objects.
模型POCO中的DateOfBirth不应该是PersianCalendar类型。日历用于操作DateTime对象,而不是DateTime对象的容器。
Your model should store the date of birth as a normal DateTime
, then you can use a PersianCalendar
to manipulate it. The easy (but a bit ugly) way to do this is to have a DateTime
field and a separate public string
property for getting and setting the date as formatted by the Persian calendar:
您的模型应将出生日期存储为正常的DateTime,然后您可以使用PersianCalendar来操作它。简单(但有点难看)的方法是使用DateTime字段和单独的公共字符串属性来获取和设置由波斯日历格式化的日期:
public class Person
{
PersianCalendar p = new PersianCalendar();
public Person()
{
}
public Person(string firstName, string lastName, DateTime birthDate)
{
FirstName = firstName;
LastName = lastName;
DateOfBirth = birthDate;
}
public Person(string firstName, string lastName, 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, 0, 0, 0);
}
}
}
Keep in mind that a DateTime
represents a general specific moment in time. It does not have any uniquely correct string representation. But if you use something like what I have above, then you are kind of deciding that for your application the only correct string representation is a Persian date, formatted as yyyy/MM/dd
.
请记住,DateTime代表一般特定时刻。它没有任何唯一正确的字符串表示形式。但是如果你使用上面的内容,那么你就可以决定对你的应用程序唯一正确的字符串表示是一个波斯日期,格式为yyyy / MM / dd。
EDIT: You can also create a custom JSON JsonConverter
and use that in your model. Your POCO model could then look like this:
编辑:您还可以创建自定义JSON JsonConverter并在模型中使用它。您的POCO模型可能如下所示:
public class Person
{
public Person() { }
public Person(string firstName, string lastName, 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;
}
And the PersianDateConverter
class looks like this:
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]), int.Parse(parts[1]), int.Parse(parts[2]), 0, 0, 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;
}
}
Note: This PersianDateConverter
is not well tested for edge cases. Seems to work ok as long as the data it is manipulating is valid though.
注意:此PersianDateConverter未针对边缘情况进行良好测试。只要它操作的数据有效,似乎工作正常。