I'm trying to create a common complex object for my models (action parameters) and reuse it in many places.
Here is some sample code:
[HttpGet("/api/values")]
public ActionResult<string> Get([FromQuery] MyModel model) {
var sb = new StringBuilder();
sb.AppendLine(model.Id);
sb.AppendLine($"{model.Id}-{model.Generated?.DateStart}-{model.Generated?.DateEnd}");
sb.AppendLine($"{model.Id}-{model.Reference?.DateStart}-{model.Reference?.DateEnd}");
return sb.ToString();
}
public class MyModel {
public string Id { get; set; }
public DateInfo Generated { get; set; } = new DateInfo();
public DateInfo Reference { get; set; } = new DateInfo();
}
public class DateInfo {
public DateTime? DateStart { get; set; }
public DateTime? DateEnd { get; set; }
public RelativeTime? RelativeTime { get; set; }
}
Imagine the DateInfo class would have validation and common properties to be used in many models.
Adding [FromQuery(Name = "Something")] to the nested properties does the trick for swagger, but it makes it impossible to have two nested properties with the same type.
UDPATE:
I understand that adding the fully qualified property name (.../values?Id=1&Generated.DateInfo=2&Reference.DateInfo=3) would make it work, but that would be a really ugly way to call any API. Hyphens are the way, not dots.
I would like to map the binding in the same way as mapping a regular property.
How to achieve that?