你的位置:首页 > 软件开发 > ASP.net > asp.net mvc web api 可跨域方法

asp.net mvc web api 可跨域方法

发布时间:2015-10-09 10:00:06
1.直接修改 web.config ,不过这是针对所有 Action。<location path="Sample.txt"> <system.webServer> <httpProtocol> <cust ...

1.直接修改 web.config ,不过这是针对所有 Action。

<location path="Sample.txt">  <system.webServer>   <httpProtocol>    <customHeaders>     <add name="Access-Control-Allow-Origin" value="*" />    </customHeaders>   </httpProtocol>  </system.webServer></location>

方法 2.加入一个类别,内容为以下所示:

using System;using System.Collections.Generic;using System.Linq;using System.Web;using System;using System.Web.Http.Filters;namespace Workflow.Filters{  public class AllowCrossSiteJsonAttribute : ActionFilterAttribute  {    public override void OnActionExecuted(HttpActionExecutedContext actionExecutedContext)    {      if (actionExecutedContext.Response != null)        actionExecutedContext.Response.Headers.Add("Access-Control-Allow-Origin", "*");      base.OnActionExecuted(actionExecutedContext);    }  }}

最后你在 Controller 或者是 Action 上面加上属性,即可允许跨网域传输数据:

原标题:asp.net mvc web api 可跨域方法

关键词:ASP.NET

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。