ASP.NET - 跨域

可以参考这篇大神文章 : http://www.cnblogs.com/daozm/articles/5362490.html 

 

在过滤器中添加允许跨域的头部信息

public class CrossSiteAttribute : ActionFilterAttribute
{
    private const string Origin = "Origin";
    /// <summary>
    /// Access-Control-Allow-Origin是HTML5中定义的一种服务器端返回Response header,用来解决资源(比如字体)的跨域权限问题。
    /// </summary>
    private const string AccessControlAllowOrigin = "Access-Control-Allow-Origin";
    /// <summary>
    ///  originHeaderdefault的值可以使 URL 或 *,如果是 URL 则只会允许来自该 URL 的请求,* 则允许任何域的请求
    /// </summary>
    private const string originHeaderdefault = "*";

    public override void OnActionExecuted(HttpActionExecutedContext actionExecutedContext)
    {
        actionExecutedContext.Response.Headers.Add(AccessControlAllowOrigin, originHeaderdefault);
    }
}

 

或者在 Web.Config 中的 System.WebSever 节点中添加

<httpProtocol>
  <customHeaders>
      <add name="Access-Control-Allow-Origin" value="*" />
      <add name="Access-Control-Allow-Headers" value="Content-Type" />
      <add name="Access-Control-Allow-Methods" value="GET, POST, PUT, DELETE, OPTIONS" />
  </customHeaders>
</httpProtocol>

 

posted @ 2017-07-18 10:46  `Laimic  阅读(120)  评论(0)    收藏  举报