I am POSTing a JSON object as a string to a service method via the fetch API. Some of the properties in the object are ISO 8601 strings with timezones (eg "StartDate": "2019-04-16T13:46:04-06:00"). That is what the string looks like before POSTing via the fetch API. The C# REST service method that I am posting to has only ([FromBody]object document) as the parameter. Upon getting to that method, the string looks like this: "StartDate": "2019-04-16T19:46:04+00:00".
Why/where is the timezone getting converted? It was a string upon POSTing to the service and is still a string there.
Something to note: this works fine when using the service locally (via localhost). When it is deployed, it does not work.
Example code (TypeScript, client-side):
postDocument() {
let doc = "{'StartDate': '2019-04-16T13:46:04-06:00'}";
let response = await fetch("[serviceURL]/api/Document/AddDocument",
{
method: "POST",
headers: {
Accept: "application/json",
"Content-Type": "application/json"
},
body: doc
});
}
Example code (server-side (in TestService), C#):
[HttpPost]
public void AddDocument([FromBody]object document)
{
// Datetime string has already been converted, no longer has timezone.
console.log(document.ToString());
// startdate here is = "2019-04-16T19:46:04+00:00"
return;//Doesn't matter what's in this method
}
Adding these lines to the Register method of my WebApiConfig in App_Start forced my service to not parse the DateTime and therefore it kept the timezone.
public static void Register(HttpConfiguration config)
{
var jsonFormatter = config.Formatters.JsonFormatter;
jsonFormatter.SerializerSettings.DateParseHandling = Newtonsoft.Json.DateParseHandling.None;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.