我无法在谷歌上找到任何关于此错误的内容,我只是想知道是否有人之前遇到过这个问题。这是我第一次构建REST Web API并将其连接到我的MVC应用程序,只是尝试使用ID进行简单的GET来构建下面的代码后的详细信息页面。
public ActionResult Details(Contact contactid)
{
Contact contact = new Contact();
HttpClient client = new HttpClient();
client.BaseAddress = new Uri("http://localhost:56194/");
//CODE BELOW IS TAKING THE CLIENT AND ALLOWS IT TO ACCEPT JSON DATA
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
ServicePointManager.ServerCertificateValidationCallback = delegate { return true; };
HttpResponseMessage response = client.GetAsync("api/contacts", contactid.ID).ContinueWith((Posttank) => Posttank.Result.EnsureSuccessStatusCode());
if (response.IsSuccessStatusCode)
{
contact = response.Content.ReadAsAsync<Contact>().Result;
}
return View(contact);
}
我的Web Api方法是:
[ResponseType(typeof(contact))]
public IHttpActionResult Getcontact(int id)
{
contact contact = db.contacts.Find(id);
if (contact == null)
{
return NotFound();
}
return Ok(contact);
}
谢谢你们,
答案 0 :(得分:-2)
HttpResponseMessage response = client.GetAsync("api/contacts/"+contactid.ID).ContinueWith((Posttank) => Posttank.Result.EnsureSuccessStatusCode());
试试这个..