一佳一

记录像1+1一样简洁的代码

导航

ASP.NET MVC4 跨域配置

Posted on 2021-08-18 18:06  一佳一  阅读(226)  评论(0编辑  收藏  举报
<system.webServer>
    
    <!--配置跨域访问-->
    <httpProtocol>
      <customHeaders>
        <add name="Access-Control-Allow-Origin" value="*" />
        <add name="Access-Control-Allow-Headers" value="Content-Type" />
        <add name="Access-Control-Allow-Methods" value="GET, POST, PUT, DELETE, OPTIONS" />
      </customHeaders>
    </httpProtocol>
  </system.webServer>

在Global.asax文件中加入

protected void Application_BeginRequest()
        {
            if (Request.Headers.AllKeys.Contains("Origin") && Request.HttpMethod == "OPTIONS")
            {
                Response.End();
            }
        }