印前检查请求404找不到.Net Web API;对印前检查请求的响应没有通过访问控制检查:它没有http ok状态

PreFlight Request 404 not found .net web api ; response to preflight request doesn#39;t pass access control check: it does not have http ok status(印前检查请求404找不到.Net Web API;对印前检查请求的响应没有通过访问控制检查:它没有http ok状态) - IT屋-程序员软件开发技术分享社
本文介绍了印前检查请求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状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

DispatcherQueue null when trying to update Ui property in ViewModel(尝试更新ViewModel中的Ui属性时DispatcherQueue为空)
Drawing over all windows on multiple monitors(在多个监视器上绘制所有窗口)
Programmatically show the desktop(以编程方式显示桌面)
c# Generic Setlt;Tgt; implementation to access objects by type(按类型访问对象的C#泛型集实现)
InvalidOperationException When using Context Injection in ASP.Net Core(在ASP.NET核心中使用上下文注入时发生InvalidOperationException)
LINQ many-to-many relationship, how to write a correct WHERE clause?(LINQ多对多关系,如何写一个正确的WHERE子句?)