• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 73
  • Last Modified:

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:

<system.webServer>
    <httpProtocol>
          <customHeaders>
              <add name="Access-Control-Allow-Origin" value="https://localhost:4200"/>
          </customHeaders>
      </httpProtocol>
  </system.webServer>

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
[EnableCors(....)]

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?

Thanks
0
axnst2
Asked:
axnst2
1 Solution
 
ambienceCommented:
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: https://github.com/aspnet/CORS

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: https://stackoverflow.com/a/40079245/1257434
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now