I'm self hosting a WCF service that needs to support inbound CORS REST traffic. So I added the Global.asax.cs file with the following code block, but the Application_BeginRequest() never fires. I also have set in my app.Config. Is there anything else I need to do and does this work for self hosted services or just services hosted via IIS?
protected void Application_BeginRequest(object sender, EventArgs e)
{
string origin = HttpContext.Current.Request.Headers["origin"];
if (!String.IsNullOrEmpty(origin)) // CORS origin?
HttpContext.Current.Response.AddHeader("Access-Control-Allow-Origin", origin);
if (HttpContext.Current.Request.HttpMethod == "OPTIONS") // CORS origin w/ options?
{
var requestedHeaders = HttpContext.Current.Request.Headers["Access-Control-Request-Headers"];
HttpContext.Current.Response.AddHeader("Access-Control-Allow-Headers", requestedHeaders);
HttpContext.Current.Response.AddHeader("Access-Control-Allow-Methods", "GET,POST,PUT,OPTIONS,DELETE");
HttpContext.Current.Response.AddHeader("Access-Control-Max-Age", "1728000");
HttpContext.Current.Response.StatusCode = 200;
HttpContext.Current.Response.End();
}
}