可以全局定义,也可以定义到controller或action上:
/// <summary>
/// 允许跨域
/// </summary>
public class AllowCrossSiteJsonAttribute : ActionFilterAttribute
{
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
filterContext.RequestContext.HttpContext.Response.AddHeader("Access-Control-Allow-Origin", "*");
base.OnActionExecuting(filterContext);
}
}
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method)]
[HttpPost]
[AllowCrossSiteJson]
public ActionResult RegisterUser(RegisterInfo model)