我将以下JSON从我的Angular
应用程序发布到.Net Core WebAPI
:
{CompanyBillingHireRateId: 4, CompanyId: 44, AccountId: null, EquipmentId: 128, EffectiveDate: Sun Dec 29 2019 00:00:00 GMT+0200 (South Africa Standard Time)}
如您所见,EffectiveDate
的值如下:EffectiveDate: Sun Dec 29 2019 00:00:00 GMT+0200 (South Africa Standard Time)
下面是我的.Net核心控制器方法:
[HttpPut("billing/hirerates/update")]
public async Task<IActionResult> UpdateNewCompanyBillingHireRateAsync([FromBody] CompanyBillingHireRateViewModel model)
{
try
{
var success = await _companyFacade.UpdateCompanyBillingHireRates_Async(model).ConfigureAwait(false);
if (success)
return Created("", success);
return StatusCode((int)HttpStatusCode.BadRequest);
}
catch (CustomException cEx)
{
await _customExceptionProcess.ProcessCustomException(cEx).ConfigureAwait(false);
return StatusCode(cEx.StatusCode, new { Message = cEx.FriendlyMessage ?? "" });
}
}
在我的.Net应用程序中,EffectiveDate
被接收为:{2019/12/28 22:00:00 +00:00}
在我的Startup.cs
中,我尝试将接口配置为忽略时区,并按原样保留接收到的日期值,并转换为.Net中对应的datetime
,但失败。以下是我的启动,cs配置:
services.AddControllers().AddNewtonsoftJson(setup =>
{
setup.SerializerSettings.ContractResolver = new DefaultContractResolver();
setup.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore;
setup.SerializerSettings.DateFormatHandling = Newtonsoft.Json.DateFormatHandling.IsoDateFormat;
setup.SerializerSettings.DateParseHandling = Newtonsoft.Json.DateParseHandling.None;
setup.SerializerSettings.DateTimeZoneHandling = Newtonsoft.Json.DateTimeZoneHandling.Local;
});
如何配置接口忽略时区,只将JSON值作为字符串读取,并解析到从角度用户界面收到的对应日期?
以下是我为我的项目解决此问题所做的工作:
由于ANGLE在浏览器中运行,而您的浏览器使用local time
,因此我相应地更改了API以处理此问题,如下所示:
services.AddControllers().AddNewtonsoftJson(setup =>
{
setup.SerializerSettings.ContractResolver = new DefaultContractResolver();
setup.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore;
setup.SerializerSettings.DateTimeZoneHandling = Newtonsoft.Json.DateTimeZoneHandling.Local;
});