Adding CORS

Learn to add CORS in ASP.NET app.

We'll cover the following

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:

  1. Run the backend project by pressing “F5” in Visual Studio.

  2. In a browser, go to the Resttesttest website. This is a browser application that we can use to check whether our REST API is accessible from a browser.

  3. Enter the path to the questions endpoint and press the “Ajax request” button. We see that the request is unsuccessful:

Get hands-on with 1200+ tech skills courses.