【发布时间】:2014-01-05 10:19:43
【问题描述】:
目前,我的 WebAPI 控制器执行以下操作:
var newUrl = _adminService.Foo(bar);
return Request.CreateResponse(HttpStatusCode.OK, newUrl);
所以客户端收到一个字符串,然后用javascript设置window.location。
这似乎是一种通过 WebAPI 重定向的 hacky 方式。我找到了这篇文章:
Redirect from asp.net web api post action
我已经实现的:
var newUrl = _adminService.Foo(bar);
var response = Request.CreateResponse(HttpStatusCode.Moved);
response.Headers.Location = new Uri(newUrl);
return response;
但现在我在 chrome 中收到以下错误(从 localhost 导航到子域时):
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.
我需要为此配置 CORS 吗?
【问题讨论】:
-
为什么要让服务(WebApi)强制客户端做任何事情?
-
@Maess 用户通过 webAPI 发布帖子,然后被重定向。与 MVC 控制器相同。
-
为什么不使用 MVC 控制器?
标签: c# asp.net-web-api cors http-redirect