This is a series of articles to discuss CORS (Cross Origin Resource Sharing) issue for both setup and consuming.
Introduction
In the previous articles, Consume Web API By MVC In .NET Core (
1), (
2), we created a ASP.NET Core MVC app and associated a Web API service in
Part I, and we made a MVC app as a client to consume Web API in the same app (origin) in
Part II.
In this series of articles, we will make a separated Web API server and consume it by a ASP.NET MVC Client in .NET Core in a different app (origin). In the first article, we will
- Briefly describ the concept of CORS
- Build the Web API server, and a separated consumer, MVC client, according to previous work;
- Access Web API from the Same Origin by MVC
- Ready for enabling CORS in the articles followed
A - Brief Discussion of CORS
Due to the fact that there is a lot of discussion online, we just make a very brief concept introduction here.
Browser security prevents a web page from making requests to a different domain than the one that served the web page. This restriction is called the same-origin policy.
Same Origin
Two URLs have the same origin if they have identical schemes, hosts, and ports (
RFC 6454).
Enable CORS
There are two ways to enable CORS,
- JSONP while making the AJAX call
- Enabling CORS in ASP.NET WEB API
The later one is safer and more flexible than earlier techniques, such as JSONP (
see).
B - Set up Separated Server and Client
From previous articles, we already have had an app with three modules (controllers), one Web API server, one MVC module, and another MVC as client to consume a Web API server. Now, we can use the previous app as either the server or client, and then create another counter part. For the simplest method, we use the MVC client from previous articles, and recreate the Web API server below (for the reader's convinience, I repeat the whole procedure without explanations),
- Step 1: Create an ASP.NET Core Web API application;
- Step 2: Set up Database;
- Step 3,:Set up Entity Model in Visual Studio Web API Project;
- Step 4: Set up a Database Context;
- Step 5: Scaffold API Controller with Action using Entity Framework;
- Step 6: Run and Test app
Step 1 - Create an ASP.NET Core Web API application
We use the current version of Visual Studio 2019 16.8 and .NET 5.0 SDK to build the app.
- Start Visual Studio and select Create a new project.
- In the Create a new project dialog, select ASP.NET Core Web Application > Next.
- In the Configure your new project dialog, enter
WebAPIStores
for Project name.
- Select Create.
- In the Create a new ASP.NET Core web application dialog, select,
- .NET Core and ASP.NET Core 5.0 in the dropdowns.
- ASP.NET Core Web Api.
- Create
Step 2 - Set up Database
We use the database table Stores created in database DB_Demo_API,
The code for the table,
Step 3 - Set up Entity Model in Visual Studio Web API Project
Right click project => add => New Folder, named as Model, then add a class Store,
Step 4 - Set up a Database Context
1. Create a new Database Context class, named DB_Demo_APIContext.cs,
2. Add the new Connection in the appsettings.json file,
3. Register the database connection context into Class starup.cs inside ConfigureServices,
Step 5 - Scaffold API Controller with Action using Entity Framework
Note, you might need to add 'Microsoft.EntityFrameworkCore.SqlServer', and 'Microsoft.EntityFrameworkCore.Design' here if you did not.
-
Select API Controller with actions, using Entity Framework, and then select Add.
![Consume Web API By MVC In .NET Core]()
Here, we make the controller the same name as previous parts, so you do not need to change code in the client MVC model.
Step 6 - Run and Test app
You will see the Swagger interface directly by using .NET Core 5.0,
C - Access Web API from a Same Origin Client
We use the app from the previous article (
Part II) as a
Same Origin Client to consume the server above by modifying one line of code to change the server address below. Then, we are ready for the CORS test.
Summary
In this article, we have made a separated Web API server, modified the existing ASP.NET MVC as a Same Origin Client to consume the Web API Server. In the next article, we will make another Web API consumer, an Angular client, and show the effect of the CORS issue. And in the third article, we will demostrate to enable CORS for different situations.