


How to Enable Cross-Origin Resource Sharing (CORS) on IIS7: A Step-by-Step Guide
Oct 27, 2024 am 02:07 AMEnabling Cross-Origin Resource Sharing on IIS7: A Comprehensive Guide
Introduction
Cross-Origin Resource Sharing (CORS) allows resources from one domain to be fetched and utilized by applications from a different origin. To enable CORS on IIS7, follow these steps:
Configuration
-
Add Custom Headers:
- Navigate to the web.config file for the hosting domain.
- Add the following custom headers within the <httpProtocol> section:
<customHeaders> <add name="Access-Control-Allow-Origin" value="*" /> <add name="Access-Control-Allow-Methods" value="GET,PUT,POST,DELETE,OPTIONS" /> <add name="Access-Control-Allow-Headers" value="Content-Type" /> </customHeaders>
Copy after login
Troubleshooting
Despite the configuration, if you still receive a 405 response, it may be due to IIS7's handling of HTTP OPTIONS.
Option 1: Modify IIS7 Handler Mappings
- Open IIS Manager.
- Navigate to Handler Mappings.
- Locate "OPTIONSVerbHandler."
- Change "ProtocolSupportModule" to "IsapiHandler."
-
Set the executable to:
%windir%\Microsoft.NET\Framework\v4.0.30319\aspnet_isapi.dll
Copy after login
Option 2: Handle OPTIONS Verb in Code
-
Override the BeginRequest method in your application code:
protected void Application_BeginRequest(object sender,EventArgs e) { HttpContext.Current.Response.AddHeader("Access-Control-Allow-Origin", "*"); if(HttpContext.Current.Request.HttpMethod == "OPTIONS") { // Handle pre-flight OPTIONS call from browser 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(); } }
Copy after login
Conclusion
Enabling CORS on IIS7 requires both configuration updates and potential troubleshooting. By following the steps outlined above, you can empower cross-origin interactions with confidence.
The above is the detailed content of How to Enable Cross-Origin Resource Sharing (CORS) on IIS7: A Step-by-Step Guide. For more information, please follow other related articles on the PHP Chinese website!

Hot Article

Hot tools Tags

Hot Article

Hot Article Tags

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Replace String Characters in JavaScript

HTTP Debugging with Node and http-console

Custom Google Search API Setup Tutorial
