Created
February 17, 2012 16:23
-
-
Save michaelcox/1854201 to your computer and use it in GitHub Desktop.
Allow Cross Site JSON in .NET MVC
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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