我有这个非常常见的问题,它说方法不允许(OPTIONS)GET请求.每当我进行API调用时,我都会收到以下错误.我有这样的设置web.config:
<system.webServer>
<modules>
<remove name="WebDAVModule"/>
</modules>
<httpProtocol>
<customHeaders>
<add name="Access-Control-Allow-Origin" value="*"/>
<add name="Access-Control-Allow-Headers" value="Origin, Authorization, X-Requested-With, Content-Type, Accept"/>
<add name="Access-Control-Allow-Methods" value="GET, POST, PUT, DELETE, OPTIONS"/>
</customHeaders>
</httpProtocol>
<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>
Run Code Online (Sandbox Code Playgroud)
我尝试使用Asp.Net.WebApi.Cors并强制执行CORS全局使用EnableCors()所有原始标头和方法,这也不起作用.
我想在Asp.net Web Api中的一个特定操作上启用CORS.这是我试图这样做的方式:
[Route("api/mycontroller/myaction")]
[HttpPost]
[EnableCors("https://example.com", "*", "post")]
public async Task<IHttpActionResult> MyAction()
{
...
}
Run Code Online (Sandbox Code Playgroud)
但是当我向路由发送OPTIONS请求时,我收到一个错误:"请求的资源不支持http方法'OPTIONS'." 我也尝试删除[HttpPost]注释无济于事.我错过了什么?