352262 发表于 2017-12-28 21:35:34

让IIS7和IIS6识别PUT和DELETE请求

  项目组最近需要开展自动化测试,针对老的Aspx页面,这个做自动化测试的成本太高,于是我们想从老代码中封装一些ashx的Restful服务出来,Restful我们使用HTTP的GET、POST、PUT、DELETE动词来解决请求的类型判断,但调用ashx的时候,请求返回405,意思是说目前的IIS配置不支持PUT或者DELETE的动词(IIS默认关闭对这些动词的支持),这里借用一下网上的图片。

  方法一:
  网上说法不一,但绝大多数是删除应用程序的Web.Config中的webdav模块,然后手动删除IIS7 模块中的WebDav模块:
  1.删除配置
  

<system.webServer>  <modules>
  <remove name="WebDAVModule" />
  </modules>
  <handlers>
  <remove name="WebDAV" />
  </handlers>
  </system.webServer>
  

  2.删除模块

  但这种方法不行。
  网上给出了另一种方法:
  方法二(我使用的是这种方法):
  修改IIS的配置文件,配置文件地址:C:\Windows\System32\inetsrv\config\applicationhost.config
  在这个文件内找到handlers标记,该标记下有所有模块的配置,修改对应模块允许的谓词即可,如:
  

<add name="SimpleHandlerFactory-ISAPI-4.0_64bit" path="*.ashx" verb="GET,HEAD,POST,DEBUG,PUT,DELETE" modules="IsapiModule" scriptProcessor="C:\Windows  

  
\Microsoft.NET\Framework64\v4.0.30319\aspnet_isapi.dll" preCondition="classicMode,runtimeVersionv4.0,bitness64" responseBufferLimit="0" />
  

  同理,如果还有什么谓词想加进来,直接在对应模块的配置内添加即可。
  IIS6的配置文件地址是:C:\WINDOWS\system32\inetsrv\MetaBase.xml
  方法三:
  有网友在stackorverflow上提供了一另一种方法:
  web.config中system.webServer添加
  

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

  <handlers>
  <remove name="WebDAV" />
  </handlers>
  

  最关键的就是在modules节点增加属性runManagedModulesForWebDavRequests="true"
  另外补充一下,在IIS6上如果要支持PUT、DELETE请求网上有介绍说先开启webDAV扩展,再设置脚本和写权限
  参考文档:
  http://www.cnblogs.com/muchengqingxin/p/6138443.html
  https://support.microsoft.com/zh-cn/help/942051/error-message-when-a-user-visits-a-website-that-is-hosted-on-a-server-that-is-running-internet-information-services-7.0-http-error-405.0---method-not-allowed
  http://stackoverflow.com/questions/6739124/iis-7-5-enable-put-and-delete-for-restful-service-extensionless
页: [1]
查看完整版本: 让IIS7和IIS6识别PUT和DELETE请求