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 上面加上属性,即可允许跨网域传输数据:
[AllowCrossSiteJson]
public class InstancesController : ApiController
{
// ...... }