.NET Core3.1 WebApi跨域处理

一、引入cors组件

  右击项目——在终端中打开——dotnet add package Microsoft.AspNetCore.Cors --version 2.0.1

 

 

 二、配置cors

  在项目根目录的Startup.cs

  (1)ConfigureServices方法  

    services.AddCors(options =>
    {
      options.AddPolicy("userLogin",
        builder => {
          builder.WithOrigins("http://10.10.10.13:1337", "http://10.10.10.13:5000", "http://10.10.10.13", "http://10.10.10.13:80", "http://localhost:80", "http://localhost:1337", "http://localhost").AllowAnyMethod().AllowAnyHeader().AllowCredentials();

      });
    });

  (2)Configure方法

     app.UseCors();

三、使用

  在前端需要调用的api接口前加上[EnableCors("userLogin")]

  

 

posted @   小严不言慢  阅读(328)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
点击右上角即可分享
微信分享提示