【问题标题】:404 for web.api cors OPTIONS404 for web.api cors 选项
【发布时间】: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

我错过了什么?

【问题讨论】:

标签: c# asp.net http asp.net-web-api cors


【解决方案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的问题

【讨论】:

    猜你喜欢
    • 2017-10-08
    • 2014-01-16
    • 2020-09-03
    • 2014-02-02
    • 1970-01-01
    • 2013-04-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多