前言
后端开启跨域模式也是经常用到的,特别是多域名的情况下就会涉及到跨域,比如前端地址为:http://webset.test.com,后端接口地址为:http://api.domin.com时,就会有跨域的问题。
而跨域问题有多种方式可以解决,比如:
1传统的jsonp
2前端通过代理
3后端通过设置
这里我们采用后端的配置来解决跨域的题
项目实战
1、添加跨域扩展服务类
添加文件名为AddCorsSetup的类,扩展方法如下:
代码可直接使用
public static void AddCorsSetup(this IServiceCollection services)
{
if (services == null) throw new ArgumentNullException(nameof(services));
bool isEnableAllIPs = DMS.Common.AppConfig.GetValue(new string[] { "Cors", "EnableAllIPs"