简体   繁体   English

从WebAPI控制器重定向到子域-是否需要CORS?

[英]Redirecting to subdomain from WebAPI controller - CORS required?

Currently, my WebAPI controller does this: 当前,我的WebAPI控制器执行以下操作:

var newUrl = _adminService.Foo(bar);
return Request.CreateResponse(HttpStatusCode.OK, newUrl);

So the client receives a string, and then sets window.location with javascript. 因此,客户端收到一个字符串,然后使用javascript设置window.location

This seems like a hacky way to redirect via WebAPI. 这似乎是通过WebAPI重定向的一种不可靠的方法。 I found this post: 我发现了这篇文章:

Redirect from asp.net web api post action 从ASP.NET Web API重定向操作

Which I've implemented: 我已经实现的:

var newUrl = _adminService.Foo(bar);
var response = Request.CreateResponse(HttpStatusCode.Moved);
response.Headers.Location = new Uri(newUrl);
return response;

But now I get the following error in chrome (when navigating from localhost to the subdomain): 但是现在我在chrome中遇到以下错误(从本地主机导航到子域时):

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

Do I need to configure CORS for this? 我是否需要为此配置CORS?

Do I need to configure CORS for this? 我是否需要为此配置CORS?

Yes, you do need if your javascript is hosted on a different domain than your Web API. 是的,如果您的JavaScript托管在与Web API不同的域上,则确实需要。

Yep, you have to use CORS but not all browser support it. 是的,您必须使用CORS,但并非所有浏览器都支持它。

here there is a compatibility table and here some Restrictions, Limitations and Workarounds for IE 8/9 这里有一个兼容性表, 这里是IE 8/9的一些限制,限制和解决方法

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM