简体   繁体   中英

Enabling CORS in Web Api 2

I have read through a lot of the stuff over enabling CORS in my web api.

I installed the NuGet Package and added this line to my WebApiConfig.cs:

config.EnableCors(new EnableCorsAttribute("*", "*", "*"));

I can't actually compile my code though because it says it does not have a definition for EnableCORS .

So... what is wrong? (Yes, I did install the CORS package using this:

Install-Package Microsoft.AspNet.WebApi.Cors

To solve this problem you need to do like this

  1. remove all custom headers from your web.config
  2. Add this reference in your web api project:

    System.Web.Http.Cors

  3. add the following line code inside the Register method of the WebApiConfig class.

    config.EnableCors();

  4. Add the following attribute in your controller(s)

    [EnableCors(origins: " ", headers: " ", methods: "*", SupportsCredentials = true)]

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