CORS in the ASP.NET Web API
The ASP.NET Web API is a HTTP service-oriented framework suitable for implementing cross-domain requests via CORS in web applications.
ASP.NET Web API is a framework that makes it easy to build HTTP services that reach a broad range of clients, including browsers and mobile devices. ASP.NET Web API is an ideal platform for building RESTful applications on the .NET Framework.
The EnableCorsAttribute
custom class contains properties for the allowed origins, HTTP
methods, request headers, response headers, and whether credentials are allowed.
The CorsMessageHandler
checks the policy in the attribute for the HTTP
method being invoked and sends the appropriate CORS response headers.
Enabling CORS in the ASP.NET Web API
When using the ASP.NET Web API, you may enable CORS globally on the IIS server using the methods already discussed.
It is preferable to add the CORS headers only on the pages, and for classes and methods, where they are needed. The next section shows how to...