“所请求的资源不支持http方法'OPTIONS'”;使用EnableCors时 [英] "The requested resource does not support http method 'OPTIONS'" when using EnableCors
问题描述
我想对Asp.net Web Api中的一项特定操作启用CORS。这是我尝试执行的操作:
I want to enable CORS on one specific action in an Asp.net Web Api. Here's how I'm trying to do it:
[Route("api/mycontroller/myaction")]
[HttpPost]
[EnableCors("https://example.com", "*", "post")]
public async Task<IHttpActionResult> MyAction()
{
...
}
但是当我向路由发送OPTIONS请求时,我得到一个错误:请求的资源不支持http方法'OPTIONS'。我也尝试删除[HttpPost]批注无济于事。
我缺少了什么?
But when I send an OPTIONS request to the route, I get back an error: "The requested resource does not support http method 'OPTIONS'." I also tried removing the [HttpPost] annotation to no avail. What am I missing?
推荐答案
您可能错过了对的更高级别的致电HttpConfiguration.EnableCors
,如此处所述: https://enable-cors.org /server_aspnet.html 。
You've probably missed the higher level call to HttpConfiguration.EnableCors
, as described here: https://enable-cors.org/server_aspnet.html.
将此代码添加到您的配置中:
Add this code to your configuration:
public static void Register(HttpConfiguration config)
{
// New code
config.EnableCors();
}
这篇关于“所请求的资源不支持http方法'OPTIONS'”;使用EnableCors时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!