PreFlight 请求 404 未找到 .net web api;对预检请求的响应未通过访问控制检查:它没有 http ok 状态
PreFlight Request 404 not found .net web api ; response to preflight request doesn't pass access control check: it does not have http ok status
所以我想在我的 .net 网站上启用 CORS API 但客户端应用程序不断收到 404 选项请求和第二个错误:
Has been blocked by CORS policy: Response to preflight request doesn’t pass access control check: it does not have http ok status
我按照微软官网的大纲一步一步的做了,还是不行。
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>
试试这个:
public static void Register(HttpConfiguration config)
{
config.EnableCors();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
}
在你的 api controller
中:
[EnableCors(origins: "*", headers: "*", methods: "*")]
public class TestController : ApiController
{
}
如果您想按操作方法启用它,请执行以下操作:
[EnableCors(origins: "*", headers: "*", methods: "*")]
public HttpResponseMessage Get() { ... }
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();
}
}
所以我想在我的 .net 网站上启用 CORS API 但客户端应用程序不断收到 404 选项请求和第二个错误:
Has been blocked by CORS policy: Response to preflight request doesn’t pass access control check: it does not have http ok status
我按照微软官网的大纲一步一步的做了,还是不行。
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>
试试这个:
public static void Register(HttpConfiguration config)
{
config.EnableCors();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
}
在你的 api controller
中:
[EnableCors(origins: "*", headers: "*", methods: "*")]
public class TestController : ApiController
{
}
如果您想按操作方法启用它,请执行以下操作:
[EnableCors(origins: "*", headers: "*", methods: "*")]
public HttpResponseMessage Get() { ... }
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();
}
}