DeleteAsync()未在MVC控制器中访问Web API删除 [英] DeleteAsync() is not Hitting the Web API Delete in MVC Controller
本文介绍了DeleteAsync()未在MVC控制器中访问Web API删除的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
[HttpPost, ActionName("DeleteEmp")]
public async Task<ActionResult> DeleteEmp(int Id)
{
using (HttpClient client = new HttpClient())
{
string Url = "http://localhost:14316/api/employee";
var uri = new Uri(string.Format(Url, Id));
var response = client.DeleteAsync(uri).Result;
if (response.IsSuccessStatusCode)
{
.......
}
return View("GetEmployee", JsonConvert.DeserializeObject<IEnumerable<Employee>>(result));
}
}
推荐答案
检查以下两行:
Check these two lines:
string Url = "http://localhost:14316/api/employee";
var uri = new Uri(string.Format(Url, Id));
你错过了什么吗? Id
有什么影响?您的api请求不包含任何 Id
。因此,我认为它没有找到任何匹配的api签名。
此外,异步函数需要await关键字。
Are you missing something? What effect does the Id
have here? Your api request does not contain any Id
. So, I assume it is therefore not finding any matching api signature.
Also, async function needs await keyword.
I认为这里的问题是await关键字不与async一起使用。请更改行
I think the problem here is the await keyword is not used with async. Please change line
var response = client.DeleteAsync(uri).Result;
to
to
var response = await client.DeleteAsync(uri).Result;
当使用异步调用时,我们应该使用等待关键字
As asynchronous calls are used, we should use await keyword
这篇关于DeleteAsync()未在MVC控制器中访问Web API删除的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文