[英]WebApi “The requested resource does not support http method 'DELETE”
Using WebApi angularjs project and trying to delete function as `使用 WebApi angularjs 项目并尝试将函数删除为`
[HttpDelete]
public String DeleteCountry(string cntryId)
{
if (cntryId != null)
{
return repoCountry.DeleteCountry(Convert.ToInt32(cntryId));
}
else
{
return "0";
}
}
js function is js函数是
$http({
method: "delete",
url: '/api/Country/DeleteCountry/',
dataType: "json",
data: { cntryId: cntryId }
}).then(function (response) {});
Here I am getting exception在这里我得到了例外
{"Message":"The requested resource does not support http method 'DELETE'."}
Insertion,update and get functionalities are working correctly.Giv a solution and why it is happening for delete only插入、更新和获取功能正常工作。给一个解决方案以及为什么它只发生在删除
Adorn your method with the Route
attribute (I see this give me more control on the routing behavior in web API) and pass your data parameters as constructor args in this format: [HttpDelete, Route("{cntryId}")
:使用Route
属性装饰您的方法(我认为这可以让我更好地控制 Web API 中的路由行为)并以以下格式将数据参数作为构造函数参数传递: [HttpDelete, Route("{cntryId}")
:
[HttpDelete, Route("{cntryId}")]
public String DeleteCountry(string cntryId)
{
//....
}
in the angular controller, you can just do this:在角度控制器中,您可以这样做:
$http.delete('/api/Country/' + cntryId).then(function (response) {
//if you're waiting some response
})
is not a webapi issue is more a the format of your query .不是 webapi 问题,而是查询的格式。 the message says that does not support http method 'DELETE'
because the webapi delete method is expecting an id as a parameter.该消息表示does not support http method 'DELETE'
因为 webapi 删除方法需要一个 id 作为参数。 and the route has the following format routeTemplate: "api/{controller}/{id}",
to resolve your issue try to use fiddler to intercept your request and ensure that your delete request is sent as '/api/Country/DeleteCountry/'+cntryId,
并且该路由具有以下格式routeTemplate: "api/{controller}/{id}",
要解决您的问题,请尝试使用 fiddler 拦截您的请求并确保您的删除请求以'/api/Country/DeleteCountry/'+cntryId,
I faced same issue and solved it finally.我遇到了同样的问题并最终解决了它。 This is simple and stupid solution.这是一个简单而愚蠢的解决方案。
Earlier code较早的代码
public String DeleteCountry(string cntryId)
Changed code更改代码
public String DeleteCountry(int id)
so just use 'int id' not 'cntryId'.所以只需使用“int id”而不是“cntryId”。 I did not use [HttpDelete] before method name but it worked.我没有在方法名称之前使用 [HttpDelete] 但它有效。
Option 1:选项 1:
$http({
method: "delete",
url: '/api/Country/Country?cntryId=' + cntryId,
}).then(function (response) {});
Option 2:选项 2:
public String DeleteDeleteCountry([FromBody]string cntryId)
{
if (cntryId != null)
{
return repoCountry.DeleteCountry(Convert.ToInt32(cntryId));
}
else
{
return "0";
}
}
Best Option:最佳选择:
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 Javascript
$http({
method: "delete",
url: '/api/country/' + cntryId,
}).then(function (response) {});
Assuming that u did not change the default routing.假设您没有更改默认路由。 Same error can happen if u fail to declare [HttpDelete] attribute for the action in webAPI.如果您未能为 webAPI 中的操作声明 [HttpDelete] 属性,则可能会发生相同的错误。 Please try following请尝试以下
[HttpDelete]
public IHttpActionResult Delete(int id)
{
}
This will usually happen if you are trying to do a PUT
or a DELETE
and you don't have the API configured to allow it.如果您尝试执行PUT
或DELETE
而您没有配置允许它的 API,这通常会发生。
This post shows you how to solve this 405 error and get it working properly.这篇文章向您展示了如何解决这个 405 错误并使其正常工作。
The easiest way to edit the ExtensionlessUrlHandler-Integrated-4.0 line in the web.config
file of the Web API project最简单的编辑 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>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.