CORS和新的ASPNET5

时间:2015-05-17 14:19:23

标签: angularjs asp.net-web-api cors

我试图通过工厂内的$ http模块向我的aspnet Web Api发出请求,例如:

http.get('http://localhost:5000/api/todo').success(function(r){ return r; }).error(function(err){ return err; });

但我收到以下错误:

XMLHttpRequest cannot load http://localhost:5000/api/todo. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin http://localhost:9000/ is therefore not allowed access.

我知道请求到达服务器,因为我在请求资源时记录了一条消息。

request arrives

我发现我必须在服务器上实现CORS。我添加了CORS中间件并配置了我的启动文件,但没有任何改变。

the code for CORS

我认为中间件应该将已配置的标头添加到响应中,但事实并非如此。 response headers

¿在这种情况下有什么问题?

1 个答案:

答案 0 :(得分:0)

好的,经过一个小小的研究。我发现 .WithOrigins(" ...")方法没有添加 Access-Control-Allow-Origin 标题,我没有&#39 ;不知道它是上下文还是什么,我刚刚提出issue。我现在用 .AllowAnyOrigin()替换它。

还为我的控制器类添加了一个属性。 [EnableCorsAttribute("AllowAll")]

当然,您需要添加 EnableCorsAttribute 类所在的命名空间。 using Microsoft.AspNet.Cors.Core;

这对我有用,希望对你有所帮助,感谢你的评论。