webApi跨域Cross问题的简单解决
新建过滤器类:
public class CrossSite { public class CrossSiteAttribute : ActionFilterAttribute { private const string Origin = "Origin"; private const string AccessControlAllowOrigin = "Access-Control-Allow-Origin"; private const string originHeaderdefault = "*"; public override void OnActionExecuted(HttpActionExecutedContext actionExecutedContext) { actionExecutedContext.Response.Headers.Add(AccessControlAllowOrigin, originHeaderdefault); } } }
在控制器方法上加上过滤器标签:
[CrossSite] public HttpResponseMessage GetExchangeChart(string filters) { HttpResponseMessage response = null; var list = BLL.GetList(filters); response = GetHttpResponseMessage(200, "OK", new { List = list }); //返回的被组织的数据 return response; }