1

This is a configuration issue on my WebAPI application.

When I pass this date as JSON from my Angular project to my WebAPI project it looks like this: created: "2019-01-09T16:16:06.149Z"

After it passes through Deserialization at WebAPI, it looks like this:enter image description here

UPDATE: Initially, I was skeptical that this was a JSON.Net thing. But, as looked at my models more carefully, I realized this IS a JSON.Net. I have a base class that contains my "Created", "Updated" fields. Address inherits this class. HomeAddress inherits Address. This must be too much for JSON.Net to handle. When I removed the Inheritance from the Base class, and put my Created and Updated members on HomeAddress. It worked! So, this question really pertains to JSON.Net's capability to merge inherited members sourced in a base class.

WebAPIConfig.cs (no JSON.Net configuration for dates):

public static class WebApiConfig
{
    public static void Register(HttpConfiguration config)
    {...}
}

WebAPI Controller:

[RoutePrefix("parents")]
public class ParentsController : ApiController
{
    [HttpPost]
    [Route("update")]
    public bool UpdateParentInfo(Parent parent)
    {
        // when I inspect  parent.Homeaddress.Created I get the DateTime().Min
    }
}

HomeAddress.cs nested in Parent.cs:

public class Parent
{
    [JsonProperty("parentID")]
    public Guid ParentID { get; set; }

    [JsonProperty("homeAddress")]
    public Address HomeAddress { get; set; }

    public Parent()
    {
        this.HomeAddress = new HomeAddress();
    }
}

HomeAddress.cs:

public class HomeAddress: Address
{
    public HomeAddress(){}
}

Address.cs

public class Address: Base
{
    [JsonProperty("addressID")]
    [Key]
    public Guid AddressID { get; set; }
    [JsonProperty("street1")]
    public string Street1 { get; set; }
    [JsonProperty("city")]
    public string City { get; set; }
    [JsonProperty("state")]
    public string State { get; set; }
    [JsonProperty("zip")]
    public string Zip { get; set; }
}

Base.cs

public class Base
{   
    [JsonProperty("created")]
    public DateTime Created { get; set; }
}

This date is in a nested object.

Can JSON.Net Detect Inherited Members -- this deep? Thanks for the help.

My short answer: No. And I'm running away from the abstraction as fast I can type. I'm putting my Created/Updated members in the HomeAddress class. And calling it a day.

LargeDachshund
  • 916
  • 3
  • 12
  • 25
  • Cannot reproduce in a simple unit test, see https://dotnetfiddle.net/ZuPbMU. Can you please [edit] your question to share a [mcve]? – dbc Jan 09 '19 at 18:28
  • What version of web API are you using? – dbc Jan 09 '19 at 18:53
  • I know their date format is different, but this is essentially the same question: https://stackoverflow.com/questions/21256132/deserializing-dates-with-dd-mm-yyyy-format-using-json-net#answer-53910170 You can get more info on C# datetime formats from here: https://learn.microsoft.com/en-us/dotnet/standard/base-types/custom-date-and-time-format-strings – peinearydevelopment Jan 09 '19 at 21:10
  • `2019-01-09T16:16:06.149Z` is a valid, well-formed iso 8601 date (you can test it e.g. at https://www.regexpal.com/97766) so configuring the Json.NET date parsing should not be necessary. See https://dotnetfiddle.net/csaskI. But what does the raw request look like? – dbc Jan 09 '19 at 23:41
  • I don't think this has anything to do with javascript (other than the "J" in "JSON"). – RobG Jan 10 '19 at 00:45

0 Answers0