简体   繁体   中英

Redirecting to subdomain from WebAPI controller - CORS required?

Currently, my WebAPI controller does this:

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

So the client receives a string, and then sets window.location with javascript.

This seems like a hacky way to redirect via WebAPI. I found this post:

Redirect from asp.net web api post action

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):

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?

Do I need to configure CORS for this?

Yes, you do need if your javascript is hosted on a different domain than your Web API.

Yep, you have to use CORS but not all browser support it.

here there is a compatibility table and here some Restrictions, Limitations and Workarounds for IE 8/9

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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