Adding CORS
CORS stands for Cross-Origin Resource Sharing and is a mechanism that uses HTTP headers to tell a browser to let a web application run at certain origins (domains) so that it has permission to access certain resources on a server at a different origin.
In this section, we will start by trying to access our REST API from a browser application and discover that it isn't accessible. We will then add and configure CORS in the REST API and verify that it is accessible from a browser application.
Let's carry out the following steps:
- Run the backend project by pressing F5 in Visual Studio.
- In a browser, browse to https://resttesttest.com/ address. This is a browser application that we can use to check whether our REST API is accessible from a browser.
- Enter the path to the
questions
endpoint and press the Ajax request button. We see that the request is unsuccessful:
...