WebAPI "The requested resource does not support http method 'DELETE"
WebApi "The requested resource does not support http method 'DELETE"
使用 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'."}
插入、更新和获取功能正在运行correctly.Giv一个解决方案以及为什么它只在删除时发生
不是 webapi 问题,而是您查询的格式问题。
该消息说 does not support http method 'DELETE'
因为 webapi delete 方法需要一个 id 作为参数。路线格式如下 routeTemplate: "api/{controller}/{id}",
要解决您的问题,请尝试使用 fiddler 拦截您的请求并确保您的删除请求作为 '/api/Country/DeleteCountry/'+cntryId,
发送
使用 Route
属性装饰您的方法(我看到这让我可以更好地控制网络中的路由行为 API)并以这种格式将您的数据参数作为构造函数参数传递:[HttpDelete, Route("{cntryId}")
:
[HttpDelete, Route("{cntryId}")]
public String DeleteCountry(string cntryId)
{
//....
}
在 angular 控制器中,您可以这样做:
$http.delete('/api/Country/' + cntryId).then(function (response) {
//if you're waiting some response
})
选项 1:
$http({
method: "delete",
url: '/api/Country/Country?cntryId=' + cntryId,
}).then(function (response) {});
选项 2:
public String DeleteDeleteCountry([FromBody]string cntryId)
{
if (cntryId != null)
{
return repoCountry.DeleteCountry(Convert.ToInt32(cntryId));
}
else
{
return "0";
}
}
最佳选择:
API
[Route("{countryId}")]
public IHttpActionResult Delete(int countryId)
{
try
{
repoCountry.DeleteCountry(countryId);
}
catch (RepoException ex)
{
if (ex == notfound)
this.NotFound();
if (ex == cantdelete)
this.Confict();
this.InternalServerError(ex.message);
}
return this.Ok();
}
Javascript
$http({
method: "delete",
url: '/api/country/' + cntryId,
}).then(function (response) {});
假设您没有更改默认路由。如果您未能在 webAPI 中为操作声明 [HttpDelete] 属性,则会发生相同的错误。请尝试关注
[HttpDelete]
public IHttpActionResult Delete(int id)
{
}
如果您尝试执行 PUT
或 DELETE
而您没有配置 API 允许它,通常会发生这种情况。
此 post 向您展示了如何解决此 405 错误并使其正常工作。
在 Web API 项目
的 web.config
文件中编辑 ExtensionlessUrlHandler-Integrated-4.0 行的最简单方法
<system.webServer>
<handlers>
<remove name="ExtensionlessUrlHandler-Integrated-4.0" />
<add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
</handlers>
</system.webServer>
我遇到了同样的问题并最终解决了。这是一个简单而愚蠢的解决方案。
较早的代码
public String DeleteCountry(string cntryId)
更改代码
public String DeleteCountry(int id)
所以只使用 'int id' 而不是 'cntryId'。我没有在方法名称之前使用 [HttpDelete],但它起作用了。
使用 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'."}
插入、更新和获取功能正在运行correctly.Giv一个解决方案以及为什么它只在删除时发生
不是 webapi 问题,而是您查询的格式问题。
该消息说 does not support http method 'DELETE'
因为 webapi delete 方法需要一个 id 作为参数。路线格式如下 routeTemplate: "api/{controller}/{id}",
要解决您的问题,请尝试使用 fiddler 拦截您的请求并确保您的删除请求作为 '/api/Country/DeleteCountry/'+cntryId,
使用 Route
属性装饰您的方法(我看到这让我可以更好地控制网络中的路由行为 API)并以这种格式将您的数据参数作为构造函数参数传递:[HttpDelete, Route("{cntryId}")
:
[HttpDelete, Route("{cntryId}")]
public String DeleteCountry(string cntryId)
{
//....
}
在 angular 控制器中,您可以这样做:
$http.delete('/api/Country/' + cntryId).then(function (response) {
//if you're waiting some response
})
选项 1:
$http({
method: "delete",
url: '/api/Country/Country?cntryId=' + cntryId,
}).then(function (response) {});
选项 2:
public String DeleteDeleteCountry([FromBody]string cntryId)
{
if (cntryId != null)
{
return repoCountry.DeleteCountry(Convert.ToInt32(cntryId));
}
else
{
return "0";
}
}
最佳选择:
API
[Route("{countryId}")]
public IHttpActionResult Delete(int countryId)
{
try
{
repoCountry.DeleteCountry(countryId);
}
catch (RepoException ex)
{
if (ex == notfound)
this.NotFound();
if (ex == cantdelete)
this.Confict();
this.InternalServerError(ex.message);
}
return this.Ok();
}
Javascript
$http({
method: "delete",
url: '/api/country/' + cntryId,
}).then(function (response) {});
假设您没有更改默认路由。如果您未能在 webAPI 中为操作声明 [HttpDelete] 属性,则会发生相同的错误。请尝试关注
[HttpDelete]
public IHttpActionResult Delete(int id)
{
}
如果您尝试执行 PUT
或 DELETE
而您没有配置 API 允许它,通常会发生这种情况。
此 post 向您展示了如何解决此 405 错误并使其正常工作。
在 Web API 项目
的web.config
文件中编辑 ExtensionlessUrlHandler-Integrated-4.0 行的最简单方法
<system.webServer>
<handlers>
<remove name="ExtensionlessUrlHandler-Integrated-4.0" />
<add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
</handlers>
</system.webServer>
我遇到了同样的问题并最终解决了。这是一个简单而愚蠢的解决方案。
较早的代码
public String DeleteCountry(string cntryId)
更改代码
public String DeleteCountry(int id)
所以只使用 'int id' 而不是 'cntryId'。我没有在方法名称之前使用 [HttpDelete],但它起作用了。