Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/cocoa/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
CORS通过ASP.NET Web API支持PUT和DELETE_Asp.net_Cross Domain_Asp.net Web Api_Cors - Fatal编程技术网

CORS通过ASP.NET Web API支持PUT和DELETE

CORS通过ASP.NET Web API支持PUT和DELETE,asp.net,cross-domain,asp.net-web-api,cors,Asp.net,Cross Domain,Asp.net Web Api,Cors,我正在使用ASP.NET Web API的最终版本来实现一个JavaScript友好的API。根据各种教程,我在我的web.config中启用了CORS: <system.webServer> <httpProtocol> <customHeaders> <add name="Access-Control-Allow-Origin" value="*" /> <add name=&

我正在使用ASP.NET Web API的最终版本来实现一个JavaScript友好的API。根据各种教程,我在我的web.config中启用了CORS:

<system.webServer>
 <httpProtocol>
  <customHeaders>
    <add name="Access-Control-Allow-Origin" value="*" />
    <add name="Access-Control-Allow-Headers" value="Content-Type" />
  </customHeaders>
 </httpProtocol>
</system.webServer>
<system.webServer>
  <modules runAllManagedModulesForAllRequests="true">
    <remove name="WebDAVModule"/>
  </modules>
  <handlers>
    <remove name="WebDAV" />
  </handlers>
</system.webServer>

使用上述方法,跨域GET和POST请求可以正常工作,但PUT和DELETE请求都会失败

镀铬:

访问控制允许方法不允许方法PUT

访问控制允许方法不允许方法删除


要跨域放置和删除谓词,是否还需要其他操作?

似乎添加了另一个自定义标题来解决此问题:

<system.webServer>
 <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>
</system.webServer>

此外,除了Nathan answer,请确保禁用了WebDAV IIS模块,并在web.config中设置了
runAllManagedModulesForAllRequests=“true”
设置:

<system.webServer>
 <httpProtocol>
  <customHeaders>
    <add name="Access-Control-Allow-Origin" value="*" />
    <add name="Access-Control-Allow-Headers" value="Content-Type" />
  </customHeaders>
 </httpProtocol>
</system.webServer>
<system.webServer>
  <modules runAllManagedModulesForAllRequests="true">
    <remove name="WebDAVModule"/>
  </modules>
  <handlers>
    <remove name="WebDAV" />
  </handlers>
</system.webServer>


如果没有这一点,(用于PUT、DELETE方法和发送附加选项请求)将无法工作。

解决WEBAPI2.2中CORS问题的非常简单的解决方案

在WebApi配置文件中添加以下内容

var cors = new EnableCorsAttribute("*", "*", "*");
Config.EnableCors(cors);
添加此文件之前,请确保删除Web.config文件中的自定义头

    <add name="Access-Control-Allow-Origin" value="*" />
    <add name="Access-Control-Allow-Credentials" value="true" />
    <add name="Access-Control-Allow-Headers" value="Origin, X-Requested-With, Content-Type, Accept, X-Token" />
    <add name="Access-Control-Allow-Methods" value="GET,PUT,POST,DELETE,OPTIONS" />

如果在WebApiconfig中同时启用了customheader和CORS,则将面临CORS错误


在WebApi配置中添加启用的cors将解决此问题。

请在部署应用程序时在web.config中使用此选项,不要在本地web.config中使用此选项。

    <system.webServer>
  <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>
 <ModSecurity enabled="false" configFile="C:\inetpub\wwwroot\owasp_crs\modsecurity.conf" />
    <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>


尝试注释
标签中的行:

您是如何在网络配置中启用CORS的?您是否也有全局CORS处理程序?还是你只在web.config中修复了CORS?我只在web.config级别实现了这个。啊,谢谢!如果web.config中没有这些代码行,我的CORS处理程序就无法工作。
WebDAV
处理程序在做什么,为什么需要删除它?@JimAho:这里有很好的解释:OMG!!!我为此花了将近7天的时间!!幸运的是我得到了它。我也遇到了类似的问题。问题只针对PUT+DELETE。终于解决了,谢谢。这是一个很好的当前解决方案,也是一个很好的解释。如前所述,请确保两种配置(WebApiConfig.cs和Web.config)都不存在,否则CORS配置将发生冲突,并导致“访问控制允许来源”标题包含多个值http://localhost:1234,*,但只允许一个。错误。此外,使用
EnableCorsAttribute
类需要以下NuGet包:这对我不起作用。仅适用于GET和POST。