Asp.net mvc Web ApI:HttpClient未调用我的Web ApI操作
首先看一下我是如何设计我的WebAPI动作的Asp.net mvc Web ApI:HttpClient未调用我的Web ApI操作,asp.net-mvc,asp.net-web-api,Asp.net Mvc,Asp.net Web Api,首先看一下我是如何设计我的WebAPI动作的 [System.Web.Http.RoutePrefix("api/Appointments")] public class AppointmentsServiceController : ApiController { [System.Web.Http.HttpGet, System.Web.Http.Route("UserAppointments/{email}")] public IHttpActionResult UserA
[System.Web.Http.RoutePrefix("api/Appointments")]
public class AppointmentsServiceController : ApiController
{
[System.Web.Http.HttpGet, System.Web.Http.Route("UserAppointments/{email}")]
public IHttpActionResult UserAppointments(string email)
{
if (!string.IsNullOrEmpty(email))
{
AppointmentsService _appservice = new AppointmentsService();
IEnumerable<Entities.Appointments> app = _appservice.GetUserWiseAppointments(email);
if (app.Count() <= 0)
{
return NotFound();
}
else
{
return Ok(app);
}
}
else
{
return BadRequest();
}
}
}
[System.Web.Http.RoutePrefix(“api/Appoints”)]
公共类任命服务控制器:ApiController
{
[System.Web.Http.HttpGet,System.Web.Http.Route(“UserAppointments/{email}”)]
公共IHttpActionResult用户约会(字符串电子邮件)
{
如果(!string.IsNullOrEmpty(电子邮件))
{
AppointmentsService_appservice=新的AppointsService();
IEnumerable app=\u appservice.GetUserWiseAppointments(电子邮件);
if(app.Count()看起来您没有等待GetAsync调用,因此在下面的if(response.issucessStatusCode)
中,if(response.issucessStatusCode)可能总是返回false。请尝试这样调用您的方法:
using (var response = (await client.GetAsync(fullAddress)).Result)
{
混合异步和阻塞调用,如.Result
var response = client.GetAsync(fullAddress).Result
及
可能会导致死锁,这可能就是它没有命中API的原因
重构代码,使其始终保持异步
这意味着使用
更新,以
var response = await client.GetAsync(fullAddress)
以及将else
语句中的内容读取到
await response.Content.ReadAsStringAsync()
参考文献
var response = await client.GetAsync(fullAddress)
await response.Content.ReadAsStringAsync()