0

I have an object defined as such

public class FilingSearchCriteria
{
    public int? FilerId { get; set; }
    public int? TypeId { get; set; }
    public DateTime? StartDate { get; set; }
    public DateTime? EndDate { get; set; }
    public bool? Legacy { get; set; }
    public bool? NoAtttachments { get; set; }
    public bool? MissingFields { get; set; }
    public bool? MissingAttachments { get; set; }
    public string DocketNumber { get; set; }
 
}

A Net Core controller function takes it as a parameter.

[Route("api/[controller]/[action]")]
public class SearchController : ControllerBase
{
  [HttpPost]
  public async Task<IEnumerable<Domain.Filing.Filing>> Filings(FilingSearchCriteria crit) => //stuff
}

Incoming request

POST http://localhost:60680/api/search/filings/ HTTP/1.1
Host: localhost:60680
Connection: keep-alive
Content-Length: 77
sec-ch-ua: "Chromium";v="88", "Google Chrome";v="88", ";Not A Brand";v="99"
sec-ch-ua-mobile: ?0
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.104 Safari/537.36
Content-Type: application/json
Accept: application/json, text/plain, */*
Origin: http://localhost:4200
Sec-Fetch-Site: same-site
Sec-Fetch-Mode: cors
Sec-Fetch-Dest: empty
Referer: http://localhost:4200/
Accept-Encoding: gzip, deflate, br
Accept-Language: en-US,en;q=0.9

{"EndDate":"2021-02-02T18:37:50.237Z","StartDate":"2021-01-26T18:37:50.237Z"}

Relevant Startup.cs code

services.AddControllers().AddJsonOptions(x => x.JsonSerializerOptions.PropertyNamingPolicy = null);

Issue is the crit param in the function ends up having all it's parameters be null. No errors.

In addition the exact same JSON serializes properly through manual usage of JsonSerializer.Deserialize as per this fiddle https://dotnetfiddle.net/xJOiCG

To add to confusion this is the only part of the app where deserialization fails like that. Everywhere else things serialize and deserialize just fine.

americanslon
  • 4,048
  • 4
  • 32
  • 57
  • why not try improving this question of yours https://stackoverflow.com/questions/66014283/partial-deserialization-web-api-core-controller ? You clearly need a datetime converter (from the original question). So why in this question don't you even post the JSON and the converter you've written? That's so confusing. It's clearly something wrong with the specific JSON you have so it ***must*** be posted for others to have a look at. BTW, the code to apply the json converter should be shared as well. – King King Feb 02 '21 at 18:48
  • JSON is right there in the body of the request, just like it was in the original question. I also don't have a datetime converter nor do I need one. Dates come from JSON generated by angular and I have already fixed the format to iso as evident by code above as well as example in the fiddle, which works. Also there is no code to apply the converter as it's done by the net core framework itself. There is no code I have written to manually handle json coming into the api. It's a new question because I believe the issue has changed. – americanslon Feb 02 '21 at 19:03
  • 1
    your model has all properties null because the `model binding` does not read from the request body, it reads the form data instead which is empty. You can apply the `[FromBody]` on the `FilingSearchCriteria crit` – King King Feb 02 '21 at 19:07
  • Thanks, that was it! Post the answer. – americanslon Feb 02 '21 at 19:10
  • you're welcome but this is so simple and really has duplicate answers. I would find a dup answer and vote to close this instead. – King King Feb 02 '21 at 19:11
  • 1
    @KingKing - maybe this one? [.Net Core Model Binding JSON Post To Web API](https://stackoverflow.com/q/45086480/3744182) – dbc Feb 02 '21 at 19:15
  • I'll keep it; what I have learned over the years is that the answer is not as important as the question often times. People think differently, people have different "code dialects" which tends to inform how they search for answers (especially when switching frameworks). I mean just look at the assbackwards two question way we have arrived at this admittedly simple answer.:) – americanslon Feb 02 '21 at 19:16
  • @dbc thanks, that seems good enough :) – King King Feb 02 '21 at 19:16
  • @americanslon - marking your question as a duplicate doesn't delete your question. Searches will still find it, and people will still read it. It only indicates that both questions have the same answer. – dbc Feb 02 '21 at 19:19
  • 1
    Ah, then yes it's a cleaner way. Thanks guys! – americanslon Feb 02 '21 at 19:20

0 Answers0