Skip to main content
 首页 » 编程设计

c#之ASP.NET Web API 默认不允许 HTTP PUT 和 DELETE

2024年02月01日25mate10pro

我无法遵循 Microsoft 提供的本教程,因为 DELETE 和 PUT 在 ASP.NET Web API 的最新脚手架上默认被阻止:

http://www.asp.net/web-api/overview/older-versions/creating-a-web-api-that-supports-crud-operations

当我尝试执行 PUT 或 DELETE 时,我最终得到来自服务器的响应:

HTTP/1.1 405 Method Not Allowed 
Cache-Control: no-cache 
Pragma: no-cache 
Allow: GET,POST 
Content-Type: application/json; charset=utf-8 
Expires: -1 
Server: Microsoft-IIS/8.0 
X-AspNet-Version: 4.0.30319 
X-SourceFiles: =?UTF-8?B?QzpcVXNlcnNcTWF0dFxEb2N1bWVudHNcV2lubm92IEdpdEh1YiBSZXBvc2l0b3JpZXNcUkVTVGZ1bC1Db21tYW5kLUNlbnRlclxBU1AuTkVULVJFU1RmdWwtQ29tbWFuZC1DZW50ZXItSW50ZXJmYWNlXFJFU1RmdWxDb21tYW5kQ2VudGVySW50ZXJmYWNlXGFwaVxWaWRlb01peFw=?= 
X-Powered-By: ASP.NET 
Date: Fri, 23 Jan 2015 17:55:05 GMT 
Content-Length: 75 
 
{"Message":"The requested resource does not support http method 'DELETE'."} 

从这个错误中我可以得出的唯一结论是我的 Web 应用程序在 Debug模式下根本不允许 PUT 和 DELETE,我不知道从这里该怎么做。

这个问题目前只在 Debug模式下发生,我还没有在生产 IIS 机器上正式部署这个构建。可以做什么来允许 PUT 和 DELETE?

这是我的 web.config 文件的样子:

<?xml version="1.0" encoding="utf-8"?> 
<!-- 
  For more information on how to configure your ASP.NET application, please visit 
  http://go.microsoft.com/fwlink/?LinkId=301879 
  --> 
<configuration> 
  <configSections> 
    <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 --> 
    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" /> 
  </configSections> 
  <connectionStrings> 
    <add name="DefaultConnection" connectionString="Data Source=(LocalDb)\v11.0;AttachDbFilename=|DataDirectory|\aspnet-RESTfulCommandCenterInterface-20141125105128.mdf;Initial Catalog=aspnet-RESTfulCommandCenterInterface-20141125105128;Integrated Security=True" 
      providerName="System.Data.SqlClient" /> 
  </connectionStrings> 
  <appSettings></appSettings> 
  <system.web> 
    <authentication mode="None" /> 
    <compilation debug="true" targetFramework="4.5" /> 
    <httpRuntime targetFramework="4.5" /> 
  </system.web> 
  <system.webServer> 
    <modules> 
      <remove name="FormsAuthentication" /> 
    </modules> 
    <handlers> 
      <remove name="ExtensionlessUrlHandler-Integrated-4.0" /> 
      <remove name="OPTIONSVerbHandler" /> 
      <remove name="TRACEVerbHandler" /> 
      <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> 
  <runtime> 
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1"> 
      <dependentAssembly> 
        <assemblyIdentity name="Microsoft.Owin" publicKeyToken="31bf3856ad364e35" /> 
        <bindingRedirect oldVersion="1.0.0.0-2.1.0.0" newVersion="2.1.0.0" /> 
      </dependentAssembly> 
      <dependentAssembly> 
        <assemblyIdentity name="Newtonsoft.Json" culture="neutral" publicKeyToken="30ad4fe6b2a6aeed" /> 
        <bindingRedirect oldVersion="0.0.0.0-6.0.0.0" newVersion="6.0.0.0" /> 
      </dependentAssembly> 
      <dependentAssembly> 
        <assemblyIdentity name="System.Web.Helpers" publicKeyToken="31bf3856ad364e35" /> 
        <bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0" /> 
      </dependentAssembly> 
      <dependentAssembly> 
        <assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" /> 
        <bindingRedirect oldVersion="1.0.0.0-5.2.0.0" newVersion="5.2.0.0" /> 
      </dependentAssembly> 
      <dependentAssembly> 
        <assemblyIdentity name="System.Web.Optimization" publicKeyToken="31bf3856ad364e35" /> 
        <bindingRedirect oldVersion="1.0.0.0-1.1.0.0" newVersion="1.1.0.0" /> 
      </dependentAssembly> 
      <dependentAssembly> 
        <assemblyIdentity name="System.Web.WebPages" publicKeyToken="31bf3856ad364e35" /> 
        <bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0" /> 
      </dependentAssembly> 
      <dependentAssembly> 
        <assemblyIdentity name="WebGrease" publicKeyToken="31bf3856ad364e35" /> 
        <bindingRedirect oldVersion="1.0.0.0-1.5.2.14234" newVersion="1.5.2.14234" /> 
      </dependentAssembly> 
    </assemblyBinding> 
  </runtime> 
  <entityFramework> 
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" /> 
    <providers> 
      <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" /> 
    </providers> 
  </entityFramework> 
</configuration> 

请您参考如下方法:

这是几天前对我的修复。

Web 分布式创作和版本控制 (WebDAV) 导致我的 Web API 不允许放置请求和删除请求。无论是否使用 Debug模式,每次发出请求时,我都会收到 405 Method Not Allowed。

将此添加到您的 Web.Config

<modules> 
  <remove name="WebDAVModule" /> 
</modules> 
 
<handlers> 
  <remove name="ExtensionlessUrlHandler-Integrated-4.0" /> 
  <remove name="OPTIONSVerbHandler" /> 
  <remove name="TRACEVerbHandler" /> 
  <remove name="WebDAV" /> 
</handlers> 

然后按照以下步骤操作:

  • 在您的 IIS 管理器中,点击您的网站,然后打开 WebDAV 创作。
  • 在 IIS 管理器的右侧,您会看到 WebDAV 设置.. 打开它。
  • 在请求过滤行为下将允许动词过滤设置为假