IamCalvinBrown.com a Blog Site

Code Examples, Consulting Advice and lots of other cool things

How to enable CORS in WCF


Familiarize yourself with these items:

1. Global.asax - Application_BeginRequest method
2. Exact url that will be requesting data

Note: http://localhost will not work. Please specify the port (ex: http://localhost:234) if you're running in development mode.

By adding this to your Global.asax, you allow the caller to hit your service and get a return. Otherwise, the status will be a 200, but with a readyState of Load.

HttpContext.Current.Response.AddHeader("Access-Control-Allow-Origin", "http://localhost:61417");
            if (HttpContext.Current.Request.HttpMethod == "OPTIONS")
            {
                HttpContext.Current.Response.AddHeader("Access-Control-Allow-Methods", "POST, PUT, DELETE");

                HttpContext.Current.Response.AddHeader("Access-Control-Allow-Headers", "Content-Type, Accept");
                HttpContext.Current.Response.AddHeader("Access-Control-Max-Age", "1728000");
                HttpContext.Current.Response.End();
            }