Skip to content

Instantly share code, notes, and snippets.

@Kashkovsky
Created June 3, 2016 13:49
Show Gist options
  • Save Kashkovsky/38f9ab76c9eb7c6a5cadb251a6bf4bac to your computer and use it in GitHub Desktop.
Save Kashkovsky/38f9ab76c9eb7c6a5cadb251a6bf4bac to your computer and use it in GitHub Desktop.
MVC Attribute allowing cross-domain requests
using System.Linq;
using System.Web.Mvc;
namespace Common.Helpers
{
public class AllowCrossDomainJsonAttribute : ActionFilterAttribute
{
const string Origin = "Origin";
const string AccessControlAllowOrigin = "Access-Control-Allow-Origin";
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
var request = filterContext.RequestContext.HttpContext.Request;
if (request.Headers != null)
{
filterContext.RequestContext.HttpContext.Response.AddHeader(AccessControlAllowOrigin, request.Headers.GetValues(Origin).First());
}
base.OnActionExecuting(filterContext);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment