Last active
March 21, 2017 16:42
-
-
Save samirahmed/84ec90c7b1c7330d498c to your computer and use it in GitHub Desktop.
Cross Site Json In MVC and WebAPI
This file contains 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
// taken from http://stackoverflow.com/questions/6290053/setting-access-control-allow-origin-in-asp-net-mvc-simplest-possible-method | |
// For MVC | |
public class AllowCrossSiteJsonAttribute : ActionFilterAttribute | |
{ | |
public override void OnActionExecuting(ActionExecutingContext filterContext) | |
{ | |
filterContext.RequestContext.HttpContext.Response.AddHeader("Access-Control-Allow-Origin", "*"); | |
base.OnActionExecuting(filterContext); | |
} | |
} | |
// FOR WEBAPI | |
using System; | |
using System.Web.Http.Filters; | |
public class AllowCrossSiteJsonAttribute : ActionFilterAttribute | |
{ | |
public override void OnActionExecuted(HttpActionExecutedContext actionExecutedContext) | |
{ | |
if (actionExecutedContext.Response != null) | |
actionExecutedContext.Response.Headers.Add("Access-Control-Allow-Origin", "*"); | |
base.OnActionExecuted(actionExecutedContext); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment