如何在自托管OWIN上为单个Web Api方法启用Cors

时间:2016-04-06 11:11:15

标签: c# cors owin self-hosting

我需要打开从另一个网站(AngularJS)调用的单个Web Api方法,我无法使其工作。

我添加了对Microsoft.Owin.CorsSystem.Web.Http.Cors

的引用

将此行添加到我的OWIN Startup类的Configuration方法中:

app.UseCors(Microsoft.Owin.Cors.CorsOptions.AllowAll);

config.EnableCors();添加到ConfigureWebApi方法

已添加

[EnableCors(origins: "*", headers: "*", methods: "*")]

在我的Web Api控制器方法之上。

我还是

XMLHttpRequest cannot load {myUrl}. Response to preflight request doesn't
pass access control check: No 'Access-Control-Allow-Origin' header is present 
on the requested resource. Origin 'http://localhost' is therefore not 
allowed access. The response had HTTP status code 401.

我尝试了上述不同的组合而没有运气。我还看到了其他回复,其中包括向web.configGlobal.asax添加代码,但我自己托管了OWIN并且没有任何这些回复。

我怎样才能让它发挥作用?

0 个答案:

没有答案