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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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:

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.