首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >默认情况下,ASP.NET web API上不允许HTTP PUT和DELETE

默认情况下,ASP.NET web API上不允许HTTP PUT和DELETE
EN

Stack Overflow用户
提问于 2015-01-24 02:03:50
回答 3查看 3.3K关注 0票数 1

我无法遵循微软提供的这篇教程,因为在最新的ASP.NET web API脚手架上,DELETE和PUT默认是被阻止的:

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

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

代码语言:javascript
复制
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应用程序不允许PUT和DELETE,我不知道从这里做什么。

此问题目前仅在调试模式下发生,我尚未在生产IIS计算机上正式部署此版本。可以做些什么来允许PUT和DELETE?

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

代码语言:javascript
复制
<?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>
EN

回答 3

Stack Overflow用户

发布于 2015-01-27 02:44:09

几天前,这对我来说是一个解决办法。

网络分布式创作和版本控制(WebDAV)导致我的Web不允许put请求和delete请求。无论是否处于调试模式,每次发出请求时都会收到不允许的405方法。

将此代码添加到您的Web.Config

代码语言:javascript
复制
<modules>
  <remove name="WebDAVModule" />
</modules>

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

然后按照以下步骤操作:

在IIS管理器中,单击您的网站,然后打开IIS管理器右侧的WebDAV

  • ,您将看到WebDAV设置..打开它。Request Filtering下的

  • 将Allow Verb Filtering设置为False<代码>H214<代码>F215
票数 4
EN

Stack Overflow用户

发布于 2015-01-27 02:59:49

我想说这不是一个骗局,因为我在这个问题上看到的每一个解决方案都说,你要么去改变你的服务器配置,要么去摆弄WebDAV。我从来没有做过任何事情,除了修改我的Web.config来让它工作(事实上,我刚刚构建的应用程序的一个要求是它需要在不手动配置服务器的情况下工作)。我正在使用的应用程序和发布的Web.config之间唯一的区别是,我的应用程序的目标是4.5.1,并且在system.webServer下有一个验证标记(发布整个system.webServer部分以获取上下文):

代码语言:javascript
复制
<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>

票数 1
EN

Stack Overflow用户

发布于 2019-05-16 23:23:27

除了更改WebDAV之外,我还成功地在我的API处理程序方法(在控制器类中)上使用了路由属性:

代码语言:javascript
复制
' PUT api/myclass/5
<HttpPut>
<Route("api/myclass/{id}", Name:="PutItem")>
Public Function PutItem(ByVal id As Integer, <FromBody()> ByVal item As MyClass) As IHttpActionResult
  ' handle PUT method here
End Function

很抱歉用VB编写示例代码,但这是我选择的ASP.NET语言:O

斯图尔特

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/28115984

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档