asp.net-mvc WebAPI 删除不起作用 - 405 Method Not Allowed
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15619075/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
WebAPI Delete not working - 405 Method Not Allowed
提问by Chris
I appreciate any help on this as the site is supposed to go live tonight!
我感谢您对此的任何帮助,因为该网站将于今晚上线!
I have a web api controller with a Delete method. The method executes fine on my local machine running IIS Express (Windows 8) but as soon as I deployed it to the live IIS server (Windows Server 2008 R2) it stopped working and throws the following error message:
我有一个带有 Delete 方法的 web api 控制器。该方法在运行 IIS Express (Windows 8) 的本地计算机上运行良好,但一旦我将其部署到实时 IIS 服务器 (Windows Server 2008 R2),它就停止工作并抛出以下错误消息:
HTTP Error 405.0 - Method Not Allowed The page you are looking for cannot be displayed because an invalid method (HTTP Verb) is being used
HTTP 错误 405.0 - 不允许的方法 由于使用了无效的方法(HTTP 动词),因此无法显示您正在查找的页面
I have looked around the web for solutions and I implemented most reasonable ones. My web config has the following settings:
我在网上寻找解决方案,并实施了最合理的解决方案。我的网络配置有以下设置:
<system.webServer>
<validation validateIntegratedModeConfiguration="false" />
<handlers>
<remove name="ExtensionlessUrlHandler-ISAPI-4.0_32bit" />
<remove name="ExtensionlessUrlHandler-ISAPI-4.0_64bit" />
<remove name="ExtensionlessUrlHandler-Integrated-4.0" />
<add name="ExtensionlessUrlHandler-ISAPI-4.0_32bit" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" modules="IsapiModule" scriptProcessor="%windir%\Microsoft.NET\Framework\v4.0.30319\aspnet_isapi.dll" preCondition="classicMode,runtimeVersionv4.0,bitness32" responseBufferLimit="0" />
<add name="ExtensionlessUrlHandler-ISAPI-4.0_64bit" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" modules="IsapiModule" scriptProcessor="%windir%\Microsoft.NET\Framework64\v4.0.30319\aspnet_isapi.dll" preCondition="classicMode,runtimeVersionv4.0,bitness64" responseBufferLimit="0" />
<add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
</handlers>
</system.webServer>
I have also tried to change the Handler Mappings and Request Filtering in IIS to no avail. Please note that the WebDAV Authoring Rules in IIS seems to be disabled.
我也尝试过更改 IIS 中的处理程序映射和请求过滤无济于事。请注意,IIS 中的 WebDAV 创作规则似乎已禁用。
Any ideas will be greatly appreciated Thanks.
任何想法将不胜感激谢谢。
回答by Chris
I found the solution eventually! If you come across the same issue, add the following to your web.config
我最终找到了解决方案!如果您遇到同样的问题,请将以下内容添加到您的 web.config
<system.webServer>
<validation validateIntegratedModeConfiguration="false"/>
<modules runAllManagedModulesForAllRequests="true">
<remove name="WebDAVModule"/> <!-- ADD THIS -->
</modules>
... rest of settings here
I hope this helps
我希望这有帮助
回答by aleha
In some cases removing it just from modules can produce next error:
在某些情况下,仅从模块中删除它会产生下一个错误:
500.21Handler "WebDAV" has a bad module "WebDAVModule" in its module list
Module: IIS Web Core Notification: ExecuteRequestHandler"
500.21处理程序“WebDAV”在其模块列表中有一个坏模块“WebDAVModule”
模块:IIS Web 核心通知:ExecuteRequestHandler”
solution was suggested here. Also need to remove it from handlers.
<system.webServer>
<modules>
<remove name="WebDAVModule" />
</modules>
<handlers>
<remove name="WebDAV" />
</handlers>
</system.webServer>
回答by Hugo Nava Kopp
In my case none of the above solutions was working. This was because I had changed the name of the parameterin my Deletemethod.
就我而言,上述解决方案均无效。这是因为我在我的方法中更改了参数的名称Delete。
I had
我有
public void Delete(string Questionid)
instead of
代替
public void Delete(string id)
I need to use the idname because that's the name that is declared in my WebApiConfigfile. Note the idname in the third and fourth lines:
我需要使用该id名称,因为这是在我的WebApiConfig文件中声明的名称。注意id第三行和第四行的名字:
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
I got this solution from here.
我从这里得到了这个解决方案。
回答by Pavel Kharibin
The Javascript for HTTP DELETEverb must be like this:
HTTPDELETE动词的 Javascript必须是这样的:
$.ajax({
**url: "/api/SomeController/" + id,**
type: "DELETE",
dataType: "json",
success: function(data, statusText) {
alert(data);
},
error: function(request, textStatus, error) {
alert(error);
debugger;
}
});
Do notuse something like this:
千万不能使用这样的:
...
data: {id:id}
...
as when you use the POSTmethod.
就像您使用该POST方法时一样。
回答by Nithin Chandran
After trying almost every solutions here this worked for me. Add this in your APIs config file
在尝试了几乎所有的解决方案之后,这对我有用。将此添加到您的 API 配置文件中
<system.webServer>
<handlers>
<remove name="WebDAV" />
<remove name="ExtensionlessUrlHandler-Integrated-4.0" />
<remove name="OPTIONSVerbHandler" />
<remove name="TRACEVerbHandler" />
<add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="*" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
</handlers>
<modules>
<remove name="WebDAVModule" />
</modules>
</system.webServer>
回答by Aamir
I also had the same problem, I am calling WebAPi and is getting this error. Adding following configuration in web.config for services solved my problem
我也遇到了同样的问题,我正在调用 WebAPi 并收到此错误。在 web.config 中为服务添加以下配置解决了我的问题
<modules runAllManagedModulesForAllRequests="true">
<remove name="WebDAVModule"/> <!-- add this -->
</modules>
in web.config file solved my problem. This is How i was calling from client side
在 web.config 文件中解决了我的问题。这就是我从客户端调用的方式
using (var client = new HttpClient())
{
client.BaseAddress = new Uri(environment.ServiceUrl);
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
HttpResponseMessage response = client.DeleteAsync("api/Producer/" + _nopProducerId).Result;
if (response.IsSuccessStatusCode)
{
string strResult = response.Content.ReadAsAsync<string>().Result;
}
}
回答by arviman
Go to applicationHost.config (usually under C:\Windows\System32\inetsrv\config) file and comment out the following line in applicationHost.config
转到 applicationHost.config(通常在 C:\Windows\System32\inetsrv\config 下)文件并注释掉 applicationHost.config 中的以下行
1)Under <handlers>:
1) 在 <handlers> 下:
<add name="WebDAV" path="*" verb="PROPFIND,PROPPATCH,MKCOL,PUT,COPY,DELETE,MOVE,LOCK,UNLOCK" modules="WebDAVModule" resourceType="Unspecified" requireAccess="None" />
2)Also comment out the following module being referred by the above handler under <modules>
2) 还要注释掉 <modules> 下上述处理程序引用的以下模块
<add name="WebDAVModule" />
回答by Reza
In my case, I missed to add {id}to the [Route("")]and I got the same error.
Adding that fixed the problem for me: [Route("{id}")]
就我而言,我错过了添加{id}到[Route("")]并且我遇到了同样的错误。添加它为我解决了问题:[Route("{id}")]
回答by Abhishek B.
If you are using IIS 7.0 or later version. This issue is mainly related to WebDAV extension module on IIS server. this happened while Using Post OR delete action.
如果您使用的是 IIS 7.0 或更高版本。此问题主要与 IIS 服务器上的 WebDAV 扩展模块有关。这发生在使用发布或删除操作时。
Please try below setting in web config
请尝试在网络配置中进行以下设置
<system.webServer>
<modules>
<remove name="WebDAVModule" />
</modules>
<handlers>
<remove name="WebDAV" />
</handlers>
</system.webServer>
回答by Paul D
I had 405 error Method Not Allowed because I had omitted to make the Delete method on the WebApi controller public.
我有 405 错误 Method Not Allowed 因为我没有将 WebApi 控制器上的 Delete 方法设为公开。
It took me a long time to find this (too long!) because I would have expected a Not Found error in this case, so I was incorrectly assuming that my Delete method was being denied.
我花了很长时间才找到这个(太长了!),因为在这种情况下我预计会出现 Not Found 错误,所以我错误地假设我的 Delete 方法被拒绝。
The reason for Not Allowed rather than Not Found is that I also had a Get method for the same route (which will be the normal case when implementing REST). The public Get function is matched by the routing and then denied because of the wrong http method.
Not Allowed 而不是 Not Found 的原因是我也有一个用于相同路由的 Get 方法(这将是实现 REST 时的正常情况)。public Get 函数被路由匹配,然后因为 http 方法错误而被拒绝。
A simple error I know but it may save someone else some time.
我知道一个简单的错误,但它可能会为其他人节省一些时间。

