Enabling Cross-Origin Resource Sharing (CORS) on IIS7
Enabling CORS on IIS7 can be a daunting task, especially if you encounter unexpected behavior such as the 405 response before the 200 response. This article aims to shed light on this issue and provide effective solutions.
Addressing the 405 Response
The 405 Method Not Allowed response can occur when IIS7 intercepts the HTTP OPTIONS request instead of your application. To resolve this:
Alternative Solution: Handling OPTIONS Verb in BeginRequest
If the above steps do not resolve the issue, you can handle the HTTP OPTIONS verb in your BeginRequest method as follows:
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(); } }
Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.
Copyright© 2022 湘ICP备2022001581号-3