【问题标题】:How to get request and response from Web Api and save it to the database如何从 Web Api 获取请求和响应并将其保存到数据库
【发布时间】:2021-11-25 17:27:34
【问题描述】:

我在下面的Controller类中有一个方法:

    [HttpGet]
    [ResponseType(typeof(RsContractCustomer))]
    [Route("api/Contract/GetCustomerData/{cardModificationId}")]
    public async Task<IHttpActionResult> GetCustomerData([FromUri] int cardModificationId)
    {
        var jsonIgnoreNullValues = JsonConvert.SerializeObject(await _bs.GetCustomer(cardModificationId), Newtonsoft.Json.Formatting.Indented, new JsonSerializerSettings
        {
            NullValueHandling = NullValueHandling.Ignore
        });

        JObject jObject = JObject.Parse(jsonIgnoreNullValues);
        return Ok(jObject); //await _bs.GetCustomerData(id));
    }

现在我想获取HttpResponseMessage 并将特定部分保存到数据库中。我应该在这些代码中添加什么或实现中间件来获得这个?

【问题讨论】:

    标签: rest asp.net-web-api2 webapi


    【解决方案1】:

    HttpReponseMessage 在客户端接收(中间件和控制器在服务器端)。

    在客户端项目中你可以这样写:

            HttpClient client = new HttpClient();
    
            HttpResponseMessage response = await client.GetAsync("http://localhost:8080/api/contract/getcustomerdata/1");
    
            var customer = await response.Content.ReadAsAsync<RsContractCustomer>();
    
            // Save to DB
    

    【讨论】:

      猜你喜欢
      • 2019-11-09
      • 1970-01-01
      • 2022-01-25
      • 1970-01-01
      • 2021-10-21
      • 1970-01-01
      • 1970-01-01
      • 2021-08-31
      • 1970-01-01
      相关资源
      最近更新 更多