159

我最近从 Visual Studio 2010 升级到了 Visual Studio 2012 RC。安装程序还会安装 IIS 8 Express,Visual Studio 现在将其用作默认 Web 服务器。

IIS 8 阻止了我使用 PUT AND DELETE 动词的 WEB API 请求。IIS 返回 405 错误,The requested resource does not support http method 'PUT'.

我知道人们过去对此有过疑问,并且在 Stack Overflow 上有几条关于它的消息。使用 IIS 7 Express 的解决方案是卸载 WebDav。不幸的是,我没有看到任何使用 IIS 8 的方法。

我尝试从 applicationhost.config 中编辑 WebDav 部分,但这没有帮助。例如,我<add name="WebDAVModule" image="%IIS_BIN%\webdav.dll" />从配置文件中删除。

我在这上面花了太长时间。必须有一种简单的方法来启用 PUT 和 DELETE?

4

21 回答 21

169

Okay. I finally got to the bottom of this. You need to jump through some hoops to get the PUT and DELETE verbs working correctly with IIS8. In fact if you install the release candidate of VS 2012 and create a new WEB API project you'll find that the sample PUT and DELETE methods return 404 errors out of the box.

To use the PUT and DELETE verbs with the Web API you need to edit %userprofile%\documents\iisexpress\config\applicationhost.config and add the verbs to the ExtensionlessUrl handler as follows:

Change this line:

<add name="ExtensionlessUrl-Integrated-4.0" path="*." verb="GET,HEAD,POST,DEBUG" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />

to:

<add name="ExtensionlessUrl-Integrated-4.0" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />

In addition to the above you should ensure WebDAV is not interfering with your requests. This can be done by commenting out the following lines from applicationhost.config.

<add name="WebDAVModule" image="%IIS_BIN%\webdav.dll" />
<add name="WebDAVModule" /> 
<add name="WebDAV" path="*" verb="PROPFIND,PROPPATCH,MKCOL,PUT,COPY,DELETE,MOVE,LOCK,UNLOCK" modules="WebDAVModule" resourceType="Unspecified" requireAccess="None" />

Also be aware that the default Web API convention is that your method name should be the same as the invoked HTTP verb. For example if you're sending an HTTP delete request your method, by default, should be named Delete.

于 2012-06-06T02:07:20.840 回答
131

如下更改您的 Web.Config 文件。它会像魅力一样。

在节点<system.webServer>中添加以下代码部分

<modules runAllManagedModulesForAllRequests="true">
  <remove name="WebDAVModule"/>
</modules>

添加后,您的 Web.Config 将如下所示

<system.webServer>
    <validation validateIntegratedModeConfiguration="false" />
    <modules runAllManagedModulesForAllRequests="true">
        <remove name="WebDAVModule"/>
    </modules>
    <httpProtocol>
    <customHeaders>
        <add name="Access-Control-Allow-Origin" value="*" />
        <add name="Access-Control-Allow-Headers" value="Content-Type" />
        <add name="Access-Control-Allow-Methods" value="GET, POST, PUT, DELETE, OPTIONS" />
    </customHeaders>
    </httpProtocol>
    <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>
于 2013-11-01T08:04:53.993 回答
64

删除WebDAV非常适合我的情况:

<modules>
  <remove name="WebDAVModule"/>
</modules>
<handlers>
  <remove name="WebDAV" />
  <remove name="ExtensionlessUrlHandler-Integrated-4.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>

最好通过 web.config 解决问题,而不是通过 iis 或 machine.config 来解决问题,如果应用程序托管在另一台机器上,则不会发生

于 2015-04-27T01:36:14.977 回答
48

更新您的 web.config

  <system.webServer>
    <modules>
      <remove name="WebDAVModule"/>
    </modules>
    <handlers>
      <remove name="WebDAV" />
      <remove name="ExtensionlessUrl-Integrated-4.0" />
      <add name="ExtensionlessUrl-Integrated-4.0"
           path="*."
           verb="GET,HEAD,POST,DEBUG,DELETE,PUT"
           type="System.Web.Handlers.TransferRequestHandler"
           preCondition="integratedMode,runtimeVersionv4.0" />
    </handlers>
  </system.webServer>

http://odetocode.com/blogs/scott/archive/2012/08/07/configuration-tips-for-asp-net-mvc-4-on-a-windows.aspx

无需修改主机配置。

于 2014-09-23T19:43:08.737 回答
18

在 Asp.Net Web API - webconfig 中。这适用于所有浏览器。

在 System.web 标签内添加以下代码

<webServices>
  <protocols>
    <add name="HttpGet"/>
    <add name="HttpPost"/>
  </protocols>
</webServices>

用以下代码替换您的 system.webserver 标记

<httpProtocol>
  <customHeaders>
    <add name="Access-Control-Allow-Origin" value="*" />
    <add name="Access-Control-Allow-Methods" value="GET,PUT,POST,DELETE" />
    <add name="Access-Control-Allow-Headers" value="Content-Type" />
  </customHeaders>
</httpProtocol>
<modules runAllManagedModulesForAllRequests="false">
  <remove name="WebDAVModule" />
</modules>

<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" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />

</handlers>

于 2013-08-27T06:06:31.213 回答
5

这与其他一些答案一起在 iis8 上对我有用。我的错误是 404.6

<system.webServer>
  <security>
  <requestFiltering>
    <verbs applyToWebDAV="false">
       <add verb="DELETE" allowed="true" />
    </verbs>
  </requestFiltering>
  </security>
</system.webServer>
于 2015-01-25T01:43:13.113 回答
5

对于可能遇到此问题的其他任何人来说,只是一个快速更新。到今天为止,更改 %userprofile%\documents\iisexpress\config\applicationhost.config 不再起作用(直到现在都可以正常工作,不确定这是否是由于 Windows 更新所致)。经过数小时的挫折后,我更改了 web.config 以将这些处理程序添加到 system.webserver 以使其工作:

<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>
于 2016-01-05T23:06:32.983 回答
5

启用 CORS(漂亮而整洁)

1.添加CORS nuget包

Install-Package microsoft.aspnet.webapi.cors

2.在 WebApiConfig.cs 文件中注册方法添加如下代码:

config.EnableCors();

例如:
使用 System.Web.Http;

namespace test
{
public static class WebApiConfig
{
    public static void Register(HttpConfiguration config)
    {
        // Web API configuration and services


        config.EnableCors(); //add this**************************


        // Web API routes
        config.MapHttpAttributeRoutes();

        config.Routes.MapHttpRoute(
            name: "DefaultApi",
            routeTemplate: "api/{controller}/{id}",
            defaults: new { id = RouteParameter.Optional }
        );           
    }
}
}

3.将波纹管代码添加到控制器的名称空间中,包括get,post,delete,put或任何http方法

[EnableCors(origins: "The address from which the request comes", headers: "*", methods: "*")]

前任:

using System.Web.Http.Cors;//add this******************************
namespace Test.Controllers
{
[EnableCors(origins: "http://localhost:53681/HTML/Restaurant.html", headers: "*", methods: "*")]
public class RestaurantController : ApiController
{
    protected TestBusinessLayer DevTestBLL = new TestBusinessLayer();

    public List<Restaurant> GET()
    {
        return DevTestBLL.GetRestaurant();
    }

    public List<Restaurant> DELETE(int id)
    {
        return DevTestBLL.DeleteRestaurant(id);
    }       
}
}

参考:http ://www.asp.net/web-api/overview/security/enabling-cross-origin-requests-in-web-api

于 2016-01-31T18:14:16.067 回答
4

对于 PHP,它很简单:

  1. 打开 IIS
  2. 转到处理程序映射
  3. 单击 php5.6.x 或 php7.0.x 上的编辑
  4. 点击“请求限制”
  5. 在动词选项卡下,选择“以下动词之一”并添加“GET、HEAD、POST、PUT、PATCH、DELETE、OPTIONS”

我想这也适用于其他处理程序。

于 2016-05-23T06:36:50.320 回答
4

在没有任何工作之后,我能够通过以下步骤解决这个问题:

• 安装IIS 时未选择“WEB DAV PUBLISHING”IIS 设置。• INETMGR - 默认网站 - 请求过滤 - HTTP 动词 - PUT 为 True

于 2016-06-15T08:44:22.710 回答
3

在无休止的搜索和尝试已经提供的答案(添加 PUT、DELETE 动词并删除 WEBdav)之后,它只是没有用。

我去了 IIS 日志记录设置:> 查看日志文件。在我的情况下,W3SVC4 是最新日期的文件夹,打开文件夹,查找最新的日志文件并看到以下条目:GET /Rejected-By-UrlScan ~/MYDOMAIN/API/ApiName/UpdateMETHOD

Update 方法用动词 GET 列出,很奇怪吧?所以我搜索了 Rejected-By-UrlScan 并找到了这个链接:UrlScan Broke My Blog

我去了这里:%windir%\system32\inetsrv\urlscan\UrlScan.ini

基本上,UrlScan 阻止了 PUT 和 DELETE 动词。我打开了这个 INI 文件,将 PUT 和 DELETE 添加到 AllowVerbs 并将它们从 DenyVerbs 列表中删除。我保存了 INI 文件,它工作了!所以对我来说,这些步骤在 ExtensionlessUrlHandler 提示旁边是必要的。

Windows 网络服务器 2008 R2(64 位),IIS 7.5。我将它与 DotNetNuke (DNN) WebAPI 结合使用。ASP.Net 4.0 我的更新方法:

[HttpPut]
[DnnAuthorize(StaticRoles = "MyRoleNames")]
public HttpResponseMessage UpdateMETHOD(DTO.MyObject myData)
于 2015-04-30T22:06:44.073 回答
3

我和你遇到了同样的问题,然后解决了,这里有解决方案,我希望它可能对你有帮助
首先

在 IISmodules配置中,循环WebDAVModule,如果您的 Web 服务器有,则将其删除

第二

在IIShandler mappings配置中,可以看到启用handler的列表,选择the PHP item,编辑,在编辑页面,点击请求限制按钮,然后the verbs tab在modal中选择,在指定要处理的动词标签,勾选all verbs radio,然后点击确定,你可能还会看到一个警告,它告诉我们使用双引号来执行 PHP-CGI,然后执行

如果完成了,然后重新启动IIS服务器,就可以了

在此处输入图像描述

于 2018-07-03T10:48:18.873 回答
2

除了上述所有解决方案之外,请检查您是否有“ id ”或 DELETE 方法中的任何自定义参数与路由配置匹配。

public void Delete(int id)
{
 //some code here
}

如果您遇到重复的 405 错误,最好将方法签名重置为默认值,然后尝试。

默认情况下,路由配置将在 URL 中查找id 。所以参数名称id在这里很重要,除非您更改App_Start文件夹下的路由配置。

不过,您可以更改id的数据类型。

例如下面的方法应该可以正常工作:

public void Delete(string id)
{
 //some code here
}

注意:还要确保通过 url 传递数据,而不是通过将负载作为正文内容携带的数据方法。

DELETE http://{url}/{action}/{id}

例子:

DELETE http://localhost/item/1

希望能帮助到你。

于 2015-11-16T12:25:35.623 回答
1

以下是使用 IIS 管理器 GUI 允许额外 HTTP 动词的方法。

  1. 在 IIS 管理器中,选择您希望允许 PUT 或 DELETE 的站点。

  2. 单击“请求过滤”选项。单击“HTTP 动词”选项卡。

  3. 单击边栏中的“允许动词...”链接。

  4. 在出现的框中键入“DELETE”,单击“确定”。

  5. 再次单击边栏中的“允许动词...”链接。

  6. 在出现的框中键入“PUT”,单击“确定”。

于 2017-01-30T18:44:31.227 回答
1

我在 MVC 应用程序中使用 ashx 文件,但上述答案均不适合我。IIS 10。

这是有效的。我没有在 IIS 或 web.config中更改“ ExtensionlessUrl-Integrated-4.0 ”,而是为“ *.ashx ”文件更改了“ SimpleHandlerFactory-Integrated-4.0 ” :

<add name="SimpleHandlerFactory-Integrated-4.0" path="*.ashx" 
verb="GET,HEAD,POST,DEBUG,PUT,DELETE" 
type="System.Web.UI.SimpleHandlerFactory" 
resourceType="Unspecified" requireAccess="Script" 
preCondition="integratedMode,runtimeVersionv4.0" />
于 2019-01-10T15:12:57.703 回答
0

另一个原因可能如下:
我根据这个答案更改了我的 Url for Web Api 方法:

Url.Action("MyAction", "MyApiCtrl", new { httproute = "" })

但是这种方法会创建如下链接:

/api/MyApiCtrl?action=MyAction

这适用于 GET 和 POST 请求,但不适用于 PUT 或 DELETE。
所以我只是将其替换为:

/api/MyApiCtrl

它解决了这个问题。

于 2014-04-25T09:03:01.493 回答
0

我不确定您是否编辑了正确的配置文件。尝试以下步骤

  1. 打开 %userprofile%\ducuments\iisexpress\config\applicationhost.config

  2. 默认情况下,给定条目在 applicationhost.config 文件中进行注释。取消注释这些条目。

<add name="WebDAVModule" image="%IIS_BIN%\webdav.dll" />


<add name="WebDAVModule" />
<add name="WebDAV" path="*"
 verb="PROPFIND,PROPPATCH,MKCOL,PUT,COPY,DELETE,MOVE,LOCK,UNLOCK"
 modules="WebDAVModule" resourceType="Unspecified" requireAccess="None"
 />
于 2012-06-06T01:54:40.353 回答
0

我加requireAccess="None"web.config喜欢这个

    <configuration>
  <location path="." inheritInChildApplications="false">
    <system.webServer>
      <handlers>
                <remove name="ExtensionlessUrlHandler-Integrated-4.0" />
                <remove name="WebDAV" />
        <add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModuleV2" resourceType="Unspecified" requireAccess="None"/>
                <add name="WebDAV" path="*" verb="PROPFIND,PROPPATCH,MKCOL,PUT,DELETE,COPY,MOVE,LOCK,UNLOCK" modules="WebDAVModule" resourceType="Unspecified" requireAccess="None" />
                <add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="GET,HEAD,POST,DEBUG,DELTE" type="System.Web.Handlers.TransferRequestHandler" resourceType="Unspecified" requireAccess="Script" preCondition="integratedMode,runtimeVersionv4.0" responseBufferLimit="0" />
      </handlers>
      <aspNetCore processPath=".\Informing.Gateway.Api.exe" stdoutLogEnabled="false" stdoutLogFile=".\logs\stdout" hostingModel="InProcess" />
    </system.webServer>
  </location>
</configuration>

它奏效了。而已

于 2021-08-28T08:59:39.307 回答
0

试错不是解决此问题的正确方法。需要做的是:

  • 识别阻止您的 PUT/DELETE 请求的来源
  • 然后你可以删除它,禁用它甚至更好地配置它:

1-为了知道来源,您必须在 IIS 中配置失败的请求跟踪。 在此处输入图像描述 2-您有“添加”要记录的错误 确保您为此跟踪条目使用较短的生命周期,以避免日志文件使您的系统不堪重负。IIS 将为每个调用生成一个单独的 xml 文件。3- 使用 Postman 或 Swagger 发出错误请求以生成跟踪错误条目。4- 在上一个屏幕中,您可以选择“查看跟踪日志”以将您带到生成的日志文件。5- 使用 Internet Explorer 打开错误的日志文件。并接受任何消息。 6-注意阻止您的请求的来源。 7-从 IIS 界面或 web.config 在模块或映射处理程序中禁用它,如其他海报所示。在此处输入图像描述 在此处输入图像描述在此处输入图像描述在此处输入图像描述

于 2021-12-30T10:28:09.737 回答
0

在 IIS 8.5/Windows 2012R2 中,这里没有提到对我有用。我不知道删除 WebDAV 是什么意思,但这并没有为我解决问题。

对我有帮助的是以下步骤;

  1. 我去了IIS管理器。
  2. 在左侧面板中选择了站点。
  3. 在左侧工作区,选择 WebDAV,双击打开。
  4. 在最右边的面板中,禁用它。

现在一切正常。

于 2017-06-08T06:07:59.307 回答
-1

您可以将您的 Delete 方法转换为 POST 为;

 [HttpPost]
 public void Delete(YourDomainModel itemToDelete)
 {
 }
于 2015-09-16T18:42:27.613 回答