我需要为我的Web API启用CORS,目前我无法升级到Framework 4.5。 (我了解System.Web.Http.Cors.EnableCorsAttribute。)
我尝试将以下内容添加到我的Web.config中以查看它是否有效,但它没有:
<system.webServer>
<httpProtocol>
<customHeaders>
<add name="Access-Control-Allow-Origin" value="*"/>
</customHeaders>
</httpProtocol>
我还尝试使用System.Web.Http.Filters.ActionFilterAttribute手动将Access-Control-Allow-Origin标头设置为“*”(基于此帖子:Add custom header to all responses in Web API) - 但是由于请求在进入操作过滤之前被拒绝,因此无效。
所以我现在有点困惑..感谢任何帮助。
编辑:结果
<add name="Access-Control-Allow-Origin" value="*"/>
一直是答案,我在测试之前一定做错了。但是这个解决方案意味着所有操作都启用了CORS(现在可以使用)。
答案 0 :(得分:10)
POST,PUT,DELETE等使用预先发布的CORS。浏览器发送OPTIONS请求。这是因为浏览器首先检查服务器端是否可以使用OPTIONS
请求处理CORS,如果成功,则发送实际请求PUT
或POST
或Delete
。由于您没有处理OPTIONS的操作方法,因此您将获得405.在最简单的形式中,您必须在控制器中实现这样的操作方法。
更多解释 - http://www.w3.org/TR/cors/#resource-preflight-requests
http://www.html5rocks.com/en/tutorials/cors/
public HttpResponseMessage Options()
{
var response = new HttpResponseMessage();
response.StatusCode = HttpStatusCode.OK;
return response;
}
注意:此操作仅响应OPTION
请求,因此您需要向web.config
添加必要的配置,例如Access-Control-Allow-Origin = *
和Access-Control-Allow-Methods = POST,PUT,DELETE
。< / p>
Web API 2
支持CORS,但使用Web API 1时,您必须遵循此路径。
答案 1 :(得分:9)
尝试添加:
<add name="Access-Control-Allow-Headers" value="*" />
答案 2 :(得分:1)
我遇到了很多关于webAPI的问题1跨域访问最终能够修复它看看我的博客http://keerthirb.blogspot.in/2017/08/making-cross-enable-for-webapi1.html
交叉代码
public class CorsHandler : DelegatingHandler
{
const string Origin = "Origin";
const string AccessControlRequestMethod = "Access-Control-Request-Method";
const string AccessControlRequestHeaders = "Access-Control-Request-Headers";
const string AccessControlAllowOrigin = "Access-Control-Allow-Origin";
const string AccessControlAllowMethods = "Access-Control-Allow-Methods";
const string AccessControlAllowHeaders = "Access-Control-Allow-Headers";
protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
{
bool isCorsRequest = request.Headers.Contains(Origin);
bool isPreflightRequest = request.Method == HttpMethod.Options;
if (isCorsRequest)
{
if (isPreflightRequest)
{
return Task.Factory.StartNew<HttpResponseMessage>(() =>
{
HttpResponseMessage response = new HttpResponseMessage(HttpStatusCode.OK);
response.Headers.Add(AccessControlAllowOrigin, request.Headers.GetValues(Origin).First());
string accessControlRequestMethod = request.Headers.GetValues(AccessControlRequestMethod).FirstOrDefault();
if (accessControlRequestMethod != null)
{
response.Headers.Add(AccessControlAllowMethods, accessControlRequestMethod);
}
string requestedHeaders = string.Join(", ", request.Headers.GetValues(AccessControlRequestHeaders));
if (!string.IsNullOrEmpty(requestedHeaders))
{
response.Headers.Add(AccessControlAllowHeaders, requestedHeaders);
}
return response;
}, cancellationToken);
}
else
{
return base.SendAsync(request, cancellationToken).ContinueWith<HttpResponseMessage>(t =>
{
HttpResponseMessage resp = t.Result;
resp.Headers.Add(AccessControlAllowOrigin, request.Headers.GetValues(Origin).First());
return resp;
});
}
}
else
{
return base.SendAsync(request, cancellationToken);
}
}
}