saf*_*sak 5 c# asp.net-web-api angularjs asp.net-web-api-routing
使用 WebApi angularjs 项目并尝试将函数删除为`
[HttpDelete]
public String DeleteCountry(string cntryId)
{
if (cntryId != null)
{
return repoCountry.DeleteCountry(Convert.ToInt32(cntryId));
}
else
{
return "0";
}
}
Run Code Online (Sandbox Code Playgroud)
js函数是
$http({
method: "delete",
url: '/api/Country/DeleteCountry/',
dataType: "json",
data: { cntryId: cntryId }
}).then(function (response) {});
Run Code Online (Sandbox Code Playgroud)
在这里我得到了例外
{"Message":"The requested resource does not support http method 'DELETE'."}
Run Code Online (Sandbox Code Playgroud)
插入、更新和获取功能正常工作。给一个解决方案以及为什么它只发生在删除
使用属性装饰你的方法Route(我发现这让我可以更好地控制 Web API 中的路由行为),并以这种格式将数据参数作为构造函数参数传递[HttpDelete, Route("{cntryId}")::
[HttpDelete, Route("{cntryId}")]
public String DeleteCountry(string cntryId)
{
//....
}
Run Code Online (Sandbox Code Playgroud)
在角度控制器中,你可以这样做:
$http.delete('/api/Country/' + cntryId).then(function (response) {
//if you're waiting some response
})
Run Code Online (Sandbox Code Playgroud)