」工欲善其事,必先利其器。「—孔子《論語.錄靈公》
首頁 > 程式設計 > 為什麼在 IIS7 上啟用 CORS 時收到 405 Method Not allowed 回應?

為什麼在 IIS7 上啟用 CORS 時收到 405 Method Not allowed 回應?

發佈於2024-12-23
瀏覽:781

Why Am I Getting a 405 Method Not Allowed Response When Enabling CORS on IIS7?

在IIS7 上啟用跨源資源共享(CORS)

在IIS7 上啟用CORS 可能是一項艱鉅的任務,尤其是當您遇到意外情況時行為,例如200 回應之前的405 回應。本文旨在闡明這個問題並提供有效的解決方案。

解決 405 回應

當 IIS7 攔截 HTTP OPTIONS 時,可能會出現 405 Method Not allowed 回應請求而不是您的應用程式。若要解決此問題:

  1. 導覽至 IIS7 中的「處理程序對應」部分。
  2. 找到「OPTIONSVerbHandler」條目。
  3. 將「ProtocolSupportModule」設定更改為「IsapiHandler」 ."
  4. 將可執行檔設定為"%windir%\Microsoft.NET\Framework\v4.0.30319\aspnet_isapi.dll."

替代解決方案:處理BeginRequest 中的OPTIONS 動詞

If上述步驟無法解決問題,您可以在 BeginRequest 方法中處理 HTTP OPTIONS 動詞,如下所示如下:

protected void Application_BeginRequest(object sender, EventArgs e)
{
    HttpContext.Current.Response.AddHeader("Access-Control-Allow-Origin", "*");

    if (HttpContext.Current.Request.HttpMethod == "OPTIONS")
    {
        // Pre-flight OPTIONS call
        HttpContext.Current.Response.AddHeader("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE");
        HttpContext.Current.Response.AddHeader("Access-Control-Allow-Headers", "Content-Type, Accept");
        HttpContext.Current.Response.AddHeader("Access-Control-Max-Age", "1728000");
        HttpContext.Current.Response.End();
    }
}
最新教學 更多>

免責聲明: 提供的所有資源部分來自互聯網,如果有侵犯您的版權或其他權益,請說明詳細緣由並提供版權或權益證明然後發到郵箱:[email protected] 我們會在第一時間內為您處理。

Copyright© 2022 湘ICP备2022001581号-3