c#webapi跨域问题 [操作系统入门]

编程

 

webconfig 中增加配置:

 

<system.webServer>

<httpProtocol>

<customHeaders>

<add name="Access-Control-Allow-Origin" value="*" />

<add name="Access-Control-Allow-Headers" value="Origin,X-Requested-With,Content-Type,accept,key" />

<add name="Access-Control-Allow-Methods" value="GET, POST, PUT, DELETE, OPTIONS" />

</customHeaders>

</httpProtocol>

</system.webServer>

 

Global.asax.cs文件中增加对于options支持

protectedvoid Application_BeginRequest(object sender, EventArgs e)

{

var req = System.Web.HttpContext.Current.Request;

if (req.HttpMethod == "OPTIONS")//过滤options请求,用于js跨域

{

Response.StatusCode = 200;

Response.SubStatusCode = 200;

Response.End();

}

}

 

c# web api 跨域问题

以上是 c#webapi跨域问题 [操作系统入门] 的全部内容, 来源链接: utcz.com/z/519478.html

回到顶部