Http GET call API endpoint with complex object from MVC controller

I need to get orders from my API made in NET Core.

The fetch is done in an action of my MVC controller, calling the endpoint of another NET Core Core APP using GET.

The API endpoint is as follows:

API:

[HttpGet("orders/orderSearchParameters")]
    public IActionResult Get(OrderSearchParameters orderSearchParameters)
    {
        var orders = MenuService.GetMenuOrders(new GetMenuOrdersRequest { From = orderSearchParameters.From, To = orderSearchParameters.To, FoodProviderId = orderSearchParameters.FoodProviderId }).Orders;
        return Ok(orders);
    }

      

The action of my MVC web application controller should call this endpoint, and for that this is the following code:

public IActionResult GetOrders(OrderSearchParametersModel orderSearchParameters)
    {
        var uri = string.Format(ApiUri + "menus/orders/");

        using (HttpClient httpClient = new HttpClient())
        {
            var response = httpClient.GetStringAsync(uri);
            if (response.IsCompleted)
            {
                var orders = JsonConvert.DeserializeObject<List<OrderModel>>(response.Result);
                return Ok(orders);
            }
            else
            {
                return BadRequest();
            }
        }            
    }

      

I cannot find how I can serialize the OrderSearchParametersModel to perform a GET operation with an HttpClient in an MVC controller.

In the attached code, I am doing a GET without an incoming object.

How can I send this object using a GET operation using HttpClient?

+3


source to share


1 answer


If you put all your parameters in the query string, they will be translated into OrderSearchParametersModel, but they must match these model property names.



+4


source







All Articles