如何从 Web Api 获取请求和响应并将其保存到数据库
How to get request and response from Web Api and save it to the database
我在下面的控制器 class 中有一个方法:
[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
并将特定部分保存到数据库中。我应该在这些代码中添加什么或实现中间件才能得到这个?
客户端接收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
我在下面的控制器 class 中有一个方法:
[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
并将特定部分保存到数据库中。我应该在这些代码中添加什么或实现中间件才能得到这个?
客户端接收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