404 for web.api cors 选项

Posted

技术标签:

【中文标题】404 for web.api cors 选项【英文标题】:404 for web.api cors OPTIONS 【发布时间】:2014-10-22 14:44:28 【问题描述】:

我已按照通常的步骤在 web.api 中启用 cors,但在 Chrome 和 Firefox 中收到 404 对 OPTIONS 请求的响应,我收到 Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at https://api.mydomain.com/api/1/widgets. This can be fixed by moving the resource to the same domain or enabling CORS.

在我的 WebApiConfig.cs 中,我有:

var enableCorsAttribute = new EnableCorsAttribute("*", "*", "*");
config.EnableCors(enableCorsAttribute);

我也尝试将EnableCors 属性添加到特定的控制器或操作,并且都具有相同的结果。

我还在 web.config 中添加了以下内容:

<modules runAllManagedModulesForAllRequests="true">
  <remove name="WebDAVModule" />
</modules>
<handlers>
    <remove name="WebDAV" />
...

这是我的 javascript

$.ajax(
    url: 'https://api.mydomain.com/api/1/widgets',
    type: "GET",
    headers: 
        Accept: "text/html; charset=utf-8",
        Authorization: 'Bearer ???????????????????????????????'
            
        );

但在 Chrome 中响应为 404,在 Firefox 中为“Cross-Origin request Blocked”。

以下是来自我的 chrome 开发人员工具栏的失败请求的详细信息:

Remote Address:??.???.???.???:443
Request URL:https://api.mydomain.com/api/1/widgets
Request Method:OPTIONS
Status Code:404 Not Found

请求

OPTIONS /api/1/widgets HTTP/1.1
Host: api.mydomain.com
Connection: keep-alive
Access-Control-Request-Method: GET
Origin: http://myotherdomain.com
User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/36.0.1985.143 Safari/537.36
Access-Control-Request-Headers: accept, authorization
Accept: */*
Referer: http://myotherdomain.com/
Accept-Encoding: gzip,deflate,sdch
Accept-Language: en-GB,en;q=0.8,en-US;q=0.6

回应

HTTP/1.1 404 Not Found
Pragma: no-cache
Content-Type: text/html; charset=utf-8
Access-Control-Allow-Origin: http://myotherdomain.com
Access-Control-Allow-Credentials: true
X-AspNetMvc-Version: 5.0
X-UA-Compatible: IE=edge,chrome=1
X-Frame-Options: SAMEORIGIN
Cache-conrol: no-store
Date: Thu, 28 Aug 2014 16:00:28 GMT
Content-Length: 341

我错过了什么?

【问题讨论】:

我在这里回答了这个问题:***.com/a/53280377/193634,IIS 正在阻止 OPTIONS 动词。 【参考方案1】:

如果其他人有同样的问题,这个问题是由于我们在 IIS 中使用了微软优秀的UrlScan。

UrlScan 有一个 AllowVerbs 部分和一个 DenyVerbs 部分。确保允许选项动词。

【讨论】:

感谢您的提示!在我的情况下,我使用MapHttpRoute 配置了我的路由,并且由于System.Web.Http.Routing.HttpMethodConstraint 而不允许使用OPTION 动词。 +5 这花了我一整天,终于赶上它了!【参考方案2】:
public static class WebApiConfig

    public static void Register(HttpConfiguration config)
    
        var corsAttr = new EnableCorsAttribute("http://localhost:3000", "*", "*");
        config.EnableCors(corsAttr);

        config.Routes.MapHttpRoute("DefaultApiWithId", "Api/controller/id", new  id = RouteParameter.Optional , new  id = new GuidConstraint() );
        config.Routes.MapHttpRoute("DefaultApiWithAction", "Api/controller/action");
        config.Routes.MapHttpRoute("DefaultApiGet", "Api/controller", new  action = "Get" , new  httpMethod = new HttpMethodConstraint(HttpMethod.Get) );
        config.Routes.MapHttpRoute("DefaultApiPost", "Api/controller", new  action = "Post" , new  httpMethod = new HttpMethodConstraint(HttpMethod.Post) );
        config.Routes.MapHttpRoute("DefaultApiOptions", "Api/controller", new  action = "Options" , new  httpMethod = new HttpMethodConstraint(HttpMethod.Options) );
    

最后一行可以解决.Net WebApi的问题

【讨论】:

以上是关于404 for web.api cors 选项的主要内容,如果未能解决你的问题,请参考以下文章

Rails 在 CORS 预检选项请求中以 404 响应

404 在 CORS 请求中返回,来自 Angular 服务的一些 Web api 调用

服务器中的 C# webapi Cors Option 请求返回错误 404 未找到

AddCors 服务选项设置不起作用.net Core 3 Web API

Web API + jQuery AJAX DELETE 请求返回 404

.NET 6.0 Web api 中的 CORS