首先在配置文件中添加下面的代码,log4net.config的那句不用管。
<add key="Access-Control-Allow-Origin" value="http://localhost:4321,http://localhost:2020,http://localhost:8701" />这句是允许跨域请求的网站,可以添加多个。
<?xml version="1.0" encoding="utf-8"?>
<appSettings>
<!-- To define the config file for the logginf function -->
<add key="log4net.Config" value="param\log.config" />
<!--If debug is true, the following account and password can be valid,
and the following ad account and ad password are in development mode.
Publishing Webapi needs to be set to false-->
<!--Access-Control-Allow-Origin-->
<add key="Access-Control-Allow-Origin" value="http://localhost:4321,http://localhost:2020,http://localhost:8701" />
<!--Access-Control-Allow-Headers-->
<add key="Access-Control-Allow-Headers" value="x-requested-with,Authorization,content-type,SiteUrl,UserId,SuperUserRole,LanID" />
</appSettings&

本文介绍了如何在 ASP.NET Web API 中启用跨域请求,通过在配置文件中设置 Access-Control-Allow-Origin,并引入 CORS 相关包,如 System.Web.Cors 和 System.Web.Http.Cors,然后在 WebApiConfig 中调用 config.EnableCors 方法。同时,文章也提醒了开发者注意防止跨站点请求伪造(CSRF)攻击的重要性。
最低0.47元/天 解锁文章
1518

被折叠的 条评论
为什么被折叠?



