在WebAPI2中获取PUT和DELETE动词以在IIS 8.5上工作 [英] Getting PUT and DELETE verbs in WebAPI2 to work on IIS 8.5

查看:89
本文介绍了在WebAPI2中获取PUT和DELETE动词以在IIS 8.5上工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图将代码发布到公司测试Web服务器上,并且在尝试使用PUT或DELETE动词调用任何方法时,从IIS返回许多错误.我已经研究了这个特定问题,并且尝试执行的所有结果都不执行任何操作,或者产生了新的错误.当我尝试使用默认的system.webServer配置时,收到常规405方法不允许的错误,这是web.config的该部分:

I am attempting to publish code to my company test web server and I'm getting a multitude of errors returned from IIS when attempting to call any methods using the PUT or DELETE verb. I've researched this particular issue, and all of the results that I've attempted either do nothing, or generate a new error. When I try with the default system.webServer configuration I receive the general 405 method not allowed error, here's that portion of the web.config:

<system.webServer>
  <validation validateIntegratedModeConfiguration="false" />
    <handlers>
      <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>
</system.webServer>

从深入研究Google和一些堆栈文章开始,大多数被接受的答案都指定要通过Web配置文件删除WebDAV,这是我完成的,下面是更新的代码:

From digging through Google and several stack articles, the majority of accepted answer specify to remove WebDAV through the web config file, which I've done, and here's that updated code:

<system.webServer>
  <modules>
    <remove name="WebDAVModule" />
  </modules
  <validation validateIntegratedModeConfiguration="false" />
    <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>
</system.webServer>

当我尝试使用仅删除WebDAV处理程序时,我继续收到405方法不允许错误,但是一旦删除WebDAVModule,我的错误就会变成500 Internal Server Error,并且从服务器查看不会实际上给我任何进一步的信息.

When I try using just removing the WebDAV handler, I continue to get the 405 method not allowed error, but as soon as I remove the WebDAVModule, my error becomes a 500 Internal Server Error, and viewing from the server doesn't actually give me any further information.

我还尝试过围绕删除和添加"ExtensionlessUrlHandler-ISAPI-4.0_32bit"& "ExtensionlessUrlHandler-ISAPI-4.0_64bit"处理程序,具有相同的结果(当不删除WebDAVModule时为405,而在我删除WebDAVModule时为500).

I've also attempted suggestions revolving around removing and adding the "ExtensionlessUrlHandler-ISAPI-4.0_32bit" & "ExtensionlessUrlHandler-ISAPI-4.0_64bit" handlers, with the same results (405 when don't remove the WebDAVModule or 500 when I do remove it).

这是我要在WebApi控制器上尝试调用的方法,也许这是问题所在,尽管它在我的开发环境中工作得很好:

Here's the methods on my WebApi controller that I'm attempting to call, maybe this is the problem, though it works just fine in my development environment:

public string PutRegistrationBatch(RegistrationBatch Model) {
  // Code to save the model to our database
}
public string DeleteRegistrationBatch(RegistrationBatch Model) {
  // Code to delete the item from the database
}

更新

我在应用程序上进行了跟踪,发现GET和POST命令可以到达该站点,但是PUT或DELETE命令都不能到达该站点:

I ran a trace on the application and I'm seeing GET and POST commands get through to the site, however none of the PUT or DELETE commands get through to the site:

  1. 2017年1月1日1:22:41/api/RegistrationBatch/200 GET
  2. 2017年1月1日1:22:55 PM/api/RegistrationBatch/200 POST

似乎PUT和DELETE命令在主机级别被过滤掉并被拒绝,而从未进入我的应用程序.

It seems as though the PUT and DELETE commands are being filtered out and refused at the host level and never making into my application.

更新

似乎WebDAV是根本问题,但是我们运行的Web服务利用了该技术,因此在我们可以更新该服务之前,将其删除是不可行的.同时,我发现Scott Hanselman在此处概述了一种解决方法( http://www.hanselman.com/blog/HTTPPUTOrDELETENotAllowedUseXHTTPMethodOverrideForYourRESTServiceWithASPNETWebAPI.aspx )允许我将PUT和DELETE请求作为POST请求发送.

It seems that WebDAV is the underlying issue, but we run a web service that utilizes this technology, so removing it is not an option until we can update that service. In the mean time, I've found that a work around outlined by Scott Hanselman here (http://www.hanselman.com/blog/HTTPPUTOrDELETENotAllowedUseXHTTPMethodOverrideForYourRESTServiceWithASPNETWebAPI.aspx) allows for me to send PUT and DELETE requests as a POST request.

我不建议这是解决该问题的方法,但是至少可以使我解决该问题,直到可以卸载WebDAV并希望允许我的Web Api 2应用程序为止正常工作.

I wouldn't suggest that is is an answer to the problem, but it at least allows me to work around the problem until such a time as we can uninstall WebDAV and hopefully that will allow my Web Api 2 application(s) to work properly.

推荐答案

我记得以前有过类似的沟渠.最终这对我有用.

I remember getting into similar ditch sometime ago. Eventually this worked for me.

将默认网站"和您的网站的所有模块配置重置为IIS中的默认值.你可以这样做 1.选择您的网站,双击模块" 2.单击右侧动作"面板下的还原为父对象"链接

Reset all module configurations of 'Default Web Site' and your website to default values in IIS. You can do this by 1. Select your website, double click "Modules" 2. Click on "Revert to Parent" link under Actions panel on right side

在Web.config中使用以下内容

Use the following into your Web.config

<system.webServer>
    <modules runAllManagedModulesForAllRequests="true" runManagedModulesForWebDavRequests="true">
      <remove name="WebDAVModule" />
    </modules>
    <handlers>
      <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>
  </system.webServer>

这篇关于在WebAPI2中获取PUT和DELETE动词以在IIS 8.5上工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆