本文介绍了印前检查请求404找不到.Net Web API;对印前检查请求的响应没有通过访问控制检查:它没有http ok状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
因此,我希望在我的.Net Web API上启用CORS,但客户端应用程序的OPTIONS请求持续显示404,并出现第二个错误:
Has been blocked by CORS policy: Response to preflight request doesn’t pass access control check: it does not have http ok status
我已一步一步地按照Microsoft网站的大纲操作,但仍未成功。
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
// Added this line to my WebApiConfig.cs file
var cors = new EnableCorsAttribute("*", "*", "*");
config.EnableCors(cors);
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
}
}
I also tried it by adding the headers in the Web.config in the httpProtocol tag file like so:
<httpProtocol>
<customHeaders>
<add name="Access-Control-Allow-Origin" value="*" />
<add name="Access-Control-Allow-Headers" value="*" />
<add name="Access-Control-Allow-Methods" value="*" />
</customHeaders>
</httpProtocol>
推荐答案
Try to enable from the web.config file under <system.webserver></system.webserver> like so:
<httpProtocol>
<customHeaders>
<add name="Access-Control-Allow-Origin" value="*" />
<add name="Access-Control-Allow-Credentials" value="true"/>
<add name="Access-Control-Allow-Headers" value="Content-Type" />
<add name="Access-Control-Allow-Methods" value="GET, POST, PUT, OPTIONS" />
</customHeaders>
</httpProtocol>
To handle the preflight request error add the line below in the <handlers></handlers> tags:
<add name="OPTIONSVerbHandler" path="*" verb="OPTIONS" modules="ProtocolSupportModule" requireAccess="None" responseBufferLimit="4194304" />
Then in the Global.asax file, add the following method:
protected void Application_BeginRequest(object sender, EventArgs e)
{
if (HttpContext.Current.Request.HttpMethod == "OPTIONS")
{
HttpContext.Current.Response.Flush();
}
}
这篇关于印前检查请求404找不到.Net Web API;对印前检查请求的响应没有通过访问控制检查:它没有http ok状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!