Getting CORS to work on C# WebAPI

There are many guides for this, and it should be a simple thing, but there is one possible complication: duplicate header entries.

So I’ll make this quick. Refer to https://docs.microsoft.com/en-us/aspnet/web-api/overview/security/enabling-cross-origin-requests-in-web-api for more information.

“Where can I get System.Web.Http.Cors?”

 

“How do I enable CORS?”

Add to WebApiConfig.Register():

 

But now, depending on your other settings, you might get a duplicate Access-Control-Allow-Origin header. This is how you remove it:

Add to WebApiApplication in Global.asax.cs:

 

That’s it. Should work now.

 

Leave a Reply

Your email address will not be published. Required fields are marked *