简体   繁体   中英

Using iis 10, how am i enabling CORS for all requests? WebAPI2

For the past 2 hours I'm trying to enable CORS in my project.

Things i tried and did not work:

  1. Enabling CORS through code:
In WebApiConfig.cs

config.EnableCors();
In MyController.cs

[EnableCors(origins: "*", headers: "*", methods: "*")]
  1. Enabling CORS trough IIS CORS module:

https://www.iis.net/downloads/microsoft/iis-cors-module

downloaded and installed. After that i tried to follow the instructions in the references yet no positive result.

How do I enable it?!

If it matters, for my client I'm using React+axios.

I'm stupid, the solution was simple:

Add those two lines to your webApiConfig.cs :

        var cors = new System.Web.Http.Cors.EnableCorsAttribute("*", "*", "*");
        config.EnableCors(cors);

Important : DO NOT combine two solutions because it will cause a "Too much cors" problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM