简体   繁体   中英

Http GET call to API endpoint with complex object from MVC controller

I have to retrieve orders from my API made in NET Core.

The retrieval is made in an action of my MVC controller calling an endpoint of another NET Core APP using GET.

The API endpoint is the following one:

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);
    }

An action of my Web App MVC controller must call that 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();
            }
        }            
    }

What I can´t find is how can I serialize OrderSearchParametersModel to perform the GET operation with the HttpClient in the MVC controller.

In the attached code I do the GET without the incoming object.

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

如果将所有参数放在查询字符串中,它们将被转换为OrderSearchParametersModel,但它们需要匹配此模型属性名称。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM