0

I would like CORS to be enabled if I build a project using the development settings, and disabled if I use the production settings.

So far I have found you can have different config files based on this answer

What I am confused about is how I would apply this for cors

Basically I would want a Web.Debug.Config with cors enabled so something like so

<customHeaders>
   <add name="Access-Control-Allow-Origin" value="*" />
 </customHeaders>

And then Would I just not add in these Headers in the web.Release.Config?

Ali
  • 633
  • 1
  • 9
  • 20

1 Answers1

3

I think there is a far easier way of achieving what you want. You could use filters over the classes/methods that you want to enable/disable cors for and then use the #IF DEBUG directives. Example:

    #if DEBUG
    [EnableCors("","","")]
    #endif
    public class MyController : ApiController 
    {
    }

For this to work you need 2 things.

  1. Add cors package from nugget
  2. EnableCors in the Register method of your config class. Simply do context.EnableCors();
Alin
  • 394
  • 5
  • 14