Implement CORS in an ASP.NET MVC application to accept requests from the same host, but different port.

I currently have an ASP.NET MVC application. I want to convert a portion of the application to a Single Page App with Angular. My plan is to create a new project in the solution that is just the Angular CLI development environment. The build output will be in a folder in my original ASP.NET project. For development I want to enable CORS to properly communicate between the two projects. Both will be served on localhost on different ports.

I've looked over solutions offered on this Stack Overflow question but it doesnt complete the picture. I've updated my web.config with the following:

              <add name="Access-Control-Allow-Origin" value="https://localhost:4200"/>

Open in new window

This allows me to hit the route but I still get a 404 with the OPTIONS preflight request. From what I've read on Stack Overflow and in the Microsoft Docs, I need to add

Open in new window

on the class or controller I want to hit. However this alwasys assumes I'm working with a Web API and not MVC.

How can I enable CORS for development only in an ASP.NET MVC project?

Who is Participating?
ambienceConnect With a Mentor Commented:
You are looking at pretty old stuff, which version of ASP.NET MVC are you using? Whats you dev env?

This is source code for the CORS implementation of ASP NET Core:

The project has a couple samples that demonstrate how to use it as well as the source code (its basically just a simple Middleware).

From the links you pasted, see this comment:
All Courses

From novice to tech pro — start learning today.