Loading

Web API的CORS

 

Web API中进行跨域需要在请求头中加入允许跨域请求

Access-Control-Allow-Origin=*

 上面代码代表允许所有跨域请求。当然也可以只允许某个站点进行跨域请求,只需将‘*’改为指定站定即可

Access-Control-Allow-Origin=“http://www.baidu.com”

当然我们可以将允许请求写成一个过滤器,即可让指定页面允许跨域请求

public class CORSFilterAttribute:AuthorizationFilterAttribute
    {
        public override void OnAuthorization(HttpActionContext actionContext)
        {
            actionContext.Request.Headers.Add("Access-Control-Allow-Origin", "*");
        }
    }

 

posted @ 2017-12-12 10:44  莫问今朝乄  阅读(208)  评论(0编辑  收藏  举报