gpt4 book ai didi

c# - WebApi "The requested resource does not support http method ' 删除”

转载 作者:太空狗 更新时间:2023-10-30 01:00:37 24 4
gpt4 key购买 nike

使用 WebApi angularjs 项目并尝试删除函数作为`

     [HttpDelete]
public String DeleteCountry(string cntryId)
{
if (cntryId != null)
{
return repoCountry.DeleteCountry(Convert.ToInt32(cntryId));

}
else
{
return "0";

}
}

js函数是

  $http({
method: "delete",
url: '/api/Country/DeleteCountry/',
dataType: "json",
data: { cntryId: cntryId }
}).then(function (response) {});

这里我得到异常

{"Message":"The requested resource does not support http method 'DELETE'."}

插入、更新和获取功能正常工作。给出一个解决方案以及为什么它只在删除时发生

最佳答案

使用 Route 属性装饰您的方法(我看到这让我可以更好地控制 Web API 中的路由行为)并以这种格式将您的数据参数作为构造函数参数传递:[HttpDelete , 路线("{cntryId}"):

[HttpDelete, Route("{cntryId}")]
public String DeleteCountry(string cntryId)
{
//....
}

在 Angular Controller 中,你可以这样做:

$http.delete('/api/Country/' + cntryId).then(function (response) {
//if you're waiting some response
})

关于c# - WebApi "The requested resource does not support http method ' 删除”,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44540539/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com