“所请求的资源不支持http方法'OPTIONS'”;使用EnableCors时 [英] "The requested resource does not support http method 'OPTIONS'" when using EnableCors

查看:363
本文介绍了“所请求的资源不支持http方法'OPTIONS'”;使用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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆