Hello I am attempting to use the HttpClient.GetAsync method to retrieve some information from my web API. I am unsure how to structure the query parameters for the GET request when needing to pass a list of complex objects. I am not necessarily tied to using query parameters, I am not sure if this would be a good case to just make it a POST and pass the data in the body? Overall I am just looking for either a better way to do this or some recommendations on how to proceed.
WEB API:
This is my WEB API code, as you can see I am passing in a list of VariantDTO and 2 simple parameters
[HttpGet]
[ResponseType(typeof(List<SchematicDTO>))]
[Route("api/schematic/GetSchematicsForMacroVariants")]
public HttpResponseMessage GetSchematicsForMacroVariants([FromUri]List<VariantDTO> dto, bool IncludeEngineered, bool IncludeStandard)
{
}
VariantDTO
This is what my DTO looks like.
public class VariantDTO
{
public string Id { get; set; }
public string Variant { get; set; }
}
MVC Controller
In my MVC controller is where I am attempting to call the WEB API. I have just started to use the asp.net web api for the first time and am getting a bit confused on how to go about passing this data?
public JsonResult GetSchematicsForSelectedVariants(List<VariantViewModel> ListOfVariants,bool GetEngineered, bool GetStandard)
{
List<SchematicViewModel> vms = new List<SchematicViewModel>(); //List of schematic vm we want to return
//Create DTO from View Model
List<VariantDTO> Dto = ListOfVariants.Select(x => new VariantDTO
{
Id = x.Id,
Variant = x.Variant
}).ToList();
using (var client = new HttpClient())
{
//---NOT SURE HOW TO PASS MY DTO and GetEngineered and GetStandard parameters????
var responseTask = client.GetAsync(ServerName + "/api/schematic/GetSchematicsForMacroVariants/");
responseTask.Wait();
var result = responseTask.Result;
if (result.IsSuccessStatusCode)
{
var readTask = result.Content.ReadAsStringAsync().Result;
//Deserialize object
var deserialized = JsonConvert.DeserializeObject<List<SchematicDTO>>(readTask);
vms = deserialized.Select(x => new SchematicViewModel
{
FullPath = x.FullPath,
Id = x.Id
}).ToList();
}
}
return Json(vms, JsonRequestBehavior.AllowGet);
}
Any direction or suggestions would be fantastic as I am a bit lost.
Thank you!