问题描述
在服务器端代码中处理它们之前,我想将从客户端收到的所有日期转换为 UTC。我正在考虑使用 ActionFilter,我想在其中找到请求有效负载中的所有日期字段并将它们转换为 UTC 日期。我知道反射有助于通过其类型查找属性,但它在匿名对象的情况下不起作用。
下面是我的代码示例
//convert body to object
var result = JsonConvert.DeserializeObject<object>(rawRequest);
var type = result.GetType();
//get collection of properties from object
var properties = result.GetType().GetProperties();
foreach (var property in properties)
{
//if data type is DateTime then convert into UTC
if (property.PropertyType == typeof(DateTime?) || property.PropertyType == typeof(DateTime))
{
string TimeZoneId = "India Standard Time";
if (HttpContext.Current.Request.Headers["TimeZoneId"] != null)
{
TimeZoneId = HttpContext.Current.Request.Headers.GetValues("TimeZoneId").FirstOrDefault();
}
var localTimeZone = TimeZoneInfo.FindSystemTimeZoneById(TimeZoneId);
var Date = DateTime.SpecifyKind(((DateTime?)property.GetValue(result,null)) ?? DateTime.Now,DateTimeKind.Unspecified);
var localTime = TimeZoneInfo.ConvertTimetoUtc(Date,localTimeZone);
}
}
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)