martes, 28 de enero de 2014

Enable CORS in ASP.NET WebAPI

In Global.asax.cs file

public class WebApiApplication : System.Web.HttpApplication
{
    protected void Application_BeginRequest(object sender, EventArgs e)
    {
        HttpContext.Current.Response.AddHeader("Access-Control-Allow-Origin", "*");
        HttpContext.Current.Response.AppendHeader("Cache-Control", "no-cache, no-store, must-revalidate"); // HTTP 1.1.
        HttpContext.Current.Response.AppendHeader("Pragma", "no-cache"); // HTTP 1.0.
        HttpContext.Current.Response.AppendHeader("Expires", "0"); // Proxies.

        HttpContext.Current.Response.AppendHeader("Cache-Control", "no-cache, no-store, must-revalidate"); // HTTP 1.1.
        HttpContext.Current.Response.AppendHeader("Pragma", "no-cache"); // HTTP 1.0.
        HttpContext.Current.Response.AppendHeader("Expires", "0"); // Proxies.

        if (HttpContext.Current.Request.HttpMethod == "OPTIONS")
        {
            HttpContext.Current.Response.AddHeader("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE");
            HttpContext.Current.Response.AddHeader("Access-Control-Allow-Headers", "Content-Type, Content-Range, Content-Disposition, Content-Description");
            HttpContext.Current.Response.End();
        }
    }
}

No hay comentarios:

Publicar un comentario