Created
June 3, 2016 13:49
-
-
Save Kashkovsky/38f9ab76c9eb7c6a5cadb251a6bf4bac to your computer and use it in GitHub Desktop.
MVC Attribute allowing cross-domain requests
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
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