Configuring cross-domain origin requests
In this recipe, you will learn how to configure and use Cross-Origin Resource Sharing (CORS) in ASP.NET Core applications.
Getting ready
To configure and use CORS, we will create two applications: one Web API application to expose a service configured with CORS constraints, and a client application that tries to consume the Web API service from a jQuery AJAX call.
How to do it...
We'll create two separate application projects, and from one of them we'll make a request to other
. The other
project is an ASP.NET Core project, and we'll enable/configure CORS in it:
- First, let's create the Web API application, by creating an empty web application:
dotnet new mvc -n Chapter11.R3.Server
- Next, we will add the ASP.NET Core MVC dependency to the project:
"Microsoft.AspNetCore.MVC": "2.0.0"
- Next, let's add the following code to
Startup.cs
. This code allows us to use Web API's controllers:
public void ConfigureServices(IServiceCollection services) { services.AddMVC(...