启用网页API 2 CORS [英] Enable CORS in Web API 2

查看:314
本文介绍了启用网页API 2 CORS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有客户端和服务器上的不同端口上运行。服务器运行的是网络API 2(V5.0.0-RC1)

I have client and a server running on different ports. The server is running Web API 2 (v5.0.0-rc1).

我尝试安装了的Microsoft ASP.NET Web API跨域支持包和使它在​​ WebApiConfig.cs 。它给我的 EnableCors()功能,所以包被正确安装。

I tried installing the Microsoft ASP.NET Web API Cross-Origin Support package and enabled it in WebApiConfig.cs. It gives me the EnableCors() function, so the package was installed correctly.

在这里,你可以看到我的寄存器()功能 WebApiConfig.cs

Here you can see my Register() function in WebApiConfig.cs:

public static void Register(HttpConfiguration config)
{
    config.MapHttpAttributeRoutes();

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

GET 请求正常工作。但是,当发送 POST ,我得到以下内容:

GET requests work fine. But when sending POST, I get the following:

OPTIONS http://localhost:19357/api/v1/rooms? 404 (Not Found) angular.js:10159
OPTIONS http://localhost:19357/api/v1/rooms? Origin http://localhost:8000 is not allowed by Access-Control-Allow-Origin. angular.js:10159
XMLHttpRequest cannot load http://localhost:19357/api/v1/rooms. Origin http://localhost:8000 is not allowed by Access-Control-Allow-Origin.

据提琴手只发送选项请求。它不发出 POST 之后。

所以我猜 config.EnableCors(CORS); WebApiConfig.cs 不是做任何事情,这导致服务器拒绝客户端/浏览器发送一个 POST 请求。

So I'm guessing the config.EnableCors(cors); in the WebApiConfig.cs isn't doing anything, which leads to the server denying the client/browser to send a POST request.

你有任何想法如何解决这个问题?

Do you have any idea how to solve this problem?

修改13年9月5日
这已被固定在5.0.0-RTM-130905

EDIT 05.09.13 This has been fixed in 5.0.0-rtm-130905

推荐答案

我最肯定打这个问题带属性的路由。 问题 WAS <一个href=\"https://aspnetwebstack.$c$cplex.com/SourceControl/changeset/6eb7f13ca6158800a18dcb299dca481277513cbd\">fixed为5.0.0-RTM-130905的。但尽管如此,你可以尝试在每晚构建其中大部分肯定会修复

I'm most definitely hitting this issue with attribute routing. The issue was fixed as of 5.0.0-rtm-130905. But still, you can try out the nightly builds which will most certainly have the fix.

要Nightlies版添加到您的NuGet软件包源,转到工具 - &GT;图书馆软件包管理器 - &GT; :包管理器设置,并在软件包源添加以下网址http://myget.org/F/aspnetwebstacknightly

To add nightlies to your NuGet package source, go to Tools -> Library Package Manager -> Package Manager Settings and add the following URL under Package Sources: http://myget.org/F/aspnetwebstacknightly

这篇关于启用网页API 2 CORS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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