[英]datetimeoffset in the asp.net core api model
i have a model like this我有一个这样的模型
class MyModel{
public DateTimeOffset plannedStartDate {get;set;}
}
and an action like this和这样的动作
[HttpPost]
public IActionResult Get(MyModel activityUpdate){
}
and i'm sending request from angular as a json我从 angular 发送请求作为 json
{
plannedStartDate: 2019-03-04T16:00:00.000Z
}
which is a valid date这是一个有效的日期
but what i'm getting in the api is wrong但我在 api 中得到的是错误的
if i inspet the variable in the immediate windows i can see that the offset couldnt' parse correctly如果我在直接窗口中检查变量,我可以看到偏移量无法正确解析
i tried to change the mvc options to我试图将 mvc 选项更改为
service.AddMvc()
.AddJsonOptions(opt=>
opt.SerializerSettings.DateParseHandling=DateParseHandling.DateTimeOffset);
which didn't help, i dont' think it's about deserialize options.这没有帮助,我认为这与反序列化选项无关。 do i have any other optiosn that writing custom modelBinder?
我还有其他选项可以编写自定义 modelBinder 吗?
If your client send request with application/json
, the controller should specify [FromBody]
to enable JsonInputFormatter
to bind the model.如果您的客户端使用
application/json
发送请求,则控制器应指定[FromBody]
以启用JsonInputFormatter
来绑定模型。
public IActionResult Get([FromBody]MyModel activityUpdate)
{
//your code.
}
For enable binding DateTimeOffset
, you could implement your own JsonInputFormatter
like要启用绑定
DateTimeOffset
,您可以实现自己的JsonInputFormatter
例如
public class DateTimeOffSetJsonInputFormatter : JsonInputFormatter
{
private readonly JsonSerializerSettings _serializerSettings;
public DateTimeOffSetJsonInputFormatter(ILogger logger
, JsonSerializerSettings serializerSettings
, ArrayPool<char> charPool
, ObjectPoolProvider objectPoolProvider
, MvcOptions options
, MvcJsonOptions jsonOptions)
: base(logger, serializerSettings, charPool, objectPoolProvider, options, jsonOptions)
{
_serializerSettings = serializerSettings;
}
public override async Task<InputFormatterResult> ReadRequestBodyAsync(InputFormatterContext context, Encoding encoding)
{
var request = context.HttpContext.Request;
using (var reader = new StreamReader(request.Body))
{
var content = await reader.ReadToEndAsync();
var resource = JObject.Parse(content);
var result = JsonConvert.DeserializeObject(resource.ToString(), context.ModelType);
foreach (var property in result.GetType().GetProperties())
{
if (property.PropertyType == typeof(DateTimeOffset))
{
property.SetValue(result, DateTimeOffset.Parse(resource[property.Name].ToString()));
}
}
return await InputFormatterResult.SuccessAsync(result);
}
}
}
Register it in Startup.cs
like在
Startup.cs
注册它,例如
services.AddMvc(mvcOptions => {
var serviceProvider = services.BuildServiceProvider();
var jsonInputLogger = serviceProvider.GetRequiredService<ILoggerFactory>().CreateLogger<DateTimeOffSetJsonInputFormatter>();
var jsonOptions = serviceProvider.GetRequiredService<IOptions<MvcJsonOptions>>().Value;
var charPool = serviceProvider.GetRequiredService<ArrayPool<char>>();
var objectPoolProvider = serviceProvider.GetRequiredService<ObjectPoolProvider>();
var customJsonInputFormatter = new DateTimeOffSetJsonInputFormatter(
jsonInputLogger,
jsonOptions.SerializerSettings,
charPool,
objectPoolProvider,
mvcOptions,
jsonOptions
);
mvcOptions.InputFormatters.Insert(0, customJsonInputFormatter);
})
.SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
Use the ISO 8601 format string: "yyyy-MM-ddTHH:mm:ssZZZ"
使用 ISO 8601 格式字符串:
"yyyy-MM-ddTHH:mm:ssZZZ"
This should bind your date properly to DateTimeOffset, and should include the offset value.这应该将您的日期正确绑定到 DateTimeOffset,并且应该包括偏移值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.