2

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!

Selthien
  • 1,178
  • 9
  • 33
  • https://anthonygiretti.com/2018/01/16/how-to-pass-in-uri-complex-objects-without-using-custom-modelbinders-or-serialize-them/ – Yehor Androsov Jul 09 '20 at 17:12
  • @YegorAndrosov Is there a better way to go about this? Would get make sense to just make this a post request and pass the data in a body parameter instead? I am not technically posting any new data so that just seems wrong. Or should I just accept using query string parameters to do this? – Selthien Jul 09 '20 at 17:42
  • You can think of in the lines of whether the 'variant' object part of the dto really required for your GetSchematicsForMacroVariants get request. If it is really needed, just check what properties of variant object are being used inside of your get request and maybe you can convert them into query parameters as well. So that your query string looks more readable. – Durga Prasad Jul 09 '20 at 18:27
  • @DurgaPrasad Every value in the dto is required for the method. Can you show an example of how to build out that query string? I think that is where I am getting confused. – Selthien Jul 09 '20 at 18:39
  • Ah, I see. You have a a list of variant dto being passed in into the get request. I guess flattening out the query string is not possible because your dto objects can vary based upon requirement. In this case I am afraid the article shared by Yegor is the right way to go ahead.If you are finding difficulty in writing out the query after going through the article, post your variant properties over here, I can give it a try. – Durga Prasad Jul 09 '20 at 19:05
  • My Variant DTO is listed above already @DurgaPrasad. Or are you needing something else? – Selthien Jul 09 '20 at 19:17
  • The 'VariantDTO' contains a 'Variant' object. I am interested in knowing it's properties – Durga Prasad Jul 09 '20 at 19:30
  • @DurgaPrasad No that is a string parameter? – Selthien Jul 09 '20 at 19:36
  • https://stackoverflow.com/q/829080/13853973. Does this help you. – Durga Prasad Jul 09 '20 at 19:42
  • @DurgaPrasad isnt there a limit on how large your query string can be? What happens if I need to pass a very large amount of data in my query string? – Selthien Jul 09 '20 at 19:54
  • There is no practical limit i guess. However it all depends on the limits set by the server or browser. A quick google search reveals that a maximum of 1024 characters is what is recommended to be on the safe side. – Durga Prasad Jul 10 '20 at 07:02

0 Answers0