面向云技术架构 - 痴者工良

  • 首页
  • 资源导航
    • 值得收藏的网站导航
    • 本站文章导航
    • 资源下载
  • 教程文档
    • kubernetes 教程
    • 多线程和异步
    • 动态编程-反射、特性、AOP
    • 表达式树
  • 隐私政策
无虑
青山一片云雾,心安即归处,山泉水洗去来时的尘土。
听风吹过松竹,自在即归处,借一壶清茶伴日出。
  1. 首页
  2. 笔记和吹水
  3. 代码片段
  4. 正文

ASP.NET Core 3.1 快速实现跨域访问

2020年4月3日 4306点热度 7人点赞 0条评论
内容纲要

Startup 中,添加一个变量

        readonly string ganweiCosr = "AllowSpecificOrigins";

Startup.ConfigureServices 中,添加服务

#if CORS
            services.AddCors(options =>
            {
                options.AddPolicy(ganweiCosr,
                builder => builder.AllowAnyHeader()
                .AllowAnyMethod()
                .AllowAnyOrigin());
            });
#endif

中间件中,有两处地方要修改


            app.UseRouting();

#if CORS
            app.UseCors(ganweiCosr);
#endif

            app.UseAuthentication();
            app.UseAuthorization();

            app.UseEndpoints(endpoints =>
            {
#if CORS
                endpoints.MapControllers().RequireCors(ganweiCosr);
#else
                endpoints.MapControllers();
#endif
            });

通过上面代码,即可实现全局跨域。

本作品采用 知识共享署名-非商业性使用-相同方式共享 4.0 国际许可协议 进行许可
标签: asp core net 跨域
最后更新:2020年4月3日

痴者工良

高级程序员劝退师

点赞
< 上一篇
下一篇 >

文章评论

取消回复
You must enable javascript to see captcha here!
目录导航

COPYRIGHT © 2022 whuanle.cn. ALL RIGHTS RESERVED.

Theme Kratos Made By Seaton Jiang

粤ICP备18051778号

粤公网安备 44030902003257号