Skip to content

Instantly share code, notes, and snippets.

@michaelcox
Created February 17, 2012 16:23
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save michaelcox/1854201 to your computer and use it in GitHub Desktop.
Save michaelcox/1854201 to your computer and use it in GitHub Desktop.
Allow Cross Site JSON in .NET MVC
public class AllowCrossSiteJson : ActionFilterAttribute
{
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
HttpContext.Current.Response.Cache.SetCacheability(HttpCacheability.NoCache);
HttpContext.Current.Response.Cache.SetNoStore();
var headers = Enumerable.ToList(HttpContext.Current.Request.Headers.AllKeys);
headers.Add("X-HTTP-Method-Override");
filterContext.RequestContext.HttpContext.Response.AppendHeader("Access-Control-Allow-Methods", "GET, POST, OPTIONS");
filterContext.RequestContext.HttpContext.Response.AppendHeader("Access-Control-Allow-Headers", string.Join(", ", headers));
filterContext.RequestContext.HttpContext.Response.AppendHeader("Access-Control-Allow-Origin", "*");
base.OnActionExecuting(filterContext);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment