简体   繁体   English

Http GET使用来自MVC控制器的复杂对象调用API端点

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

I have to retrieve orders from my API made in NET Core. 我必须从我在.NET Core中制作的API中检索订单。

The retrieval is made in an action of my MVC controller calling an endpoint of another NET Core APP using GET. 检索是在我的MVC控制器使用GET调用另一个NET核心APP的端点的动作中进行的。

The API endpoint is the following one: API端点如下:

API: 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: 我的Web App MVC控制器的操作必须调用该端点,为此,这是以下代码:

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. 我无法找到的是如何序列化OrderSearchParametersModel以使用MVC控制器中的HttpClient执行GET操作。

In the attached code I do the GET without the incoming object. 在附加的代码中,我执行GET而没有传入的对象。

How can I send this object using GET operation with HttpClient? 如何使用HttpClient使用GET操作发送此对象?

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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