Implementing CORS in an ASP.NET Core Web API
In this detailed guide, let's talk about what is CORS and how do we enable CORS in ASP.NET Core with an illustrating example
When developing client-server interactive applications where the Frontend sends a HTTP request to the Backend API for data, systems are designed such that the Frontend and Backend are deployed in separate domains to facilitate scaling independently.
There can be situations where the requests from the Frontend may fail with an error - No 'Access-Control-Allow-Origin' header is present on the requested resource
This is called a Cross-Origin Request issue or shortly, a CORS issue.
Cross Origin Resource Sharing or simply called CORS is a mechanism that governs access of resources between two components over the web.
It is a policy defined by web applications that specifies how a resource hosted under a domain can be accessed by another component out of its own domain over HTTP.
I want to share with you an article about CORS, where I have explained in detail about the context of a Preflight Request, CORS policy and the best practices around it.
I have also included sample code on how to implement a CORS policy using ASP.NET Core.
You can find out the post here - https://referbruv.com/blog/setting-up-and-configuring-cors-in-an-aspnet-core-api/