• .NET Core Web API 跨域请求


    使用.net core 3.0 搭建的Web API接口,使用另一个服务器访问时候报这个跨域请求错误:

     

    解决方法如下:

    1、在web api 项目的startup类的ConfigureServices方法中加入以下内容。

            /// <summary>
            /// 
            /// </summary>
            /// <param name="services"></param>
            // This method gets called by the runtime. Use this method to add services to the container.
            public void ConfigureServices(IServiceCollection services)
            {
                services.AddControllers();
                //
                services.AddCors(options =>
                {
                    options.AddPolicy("AllowSpecificOrigins",
                        builder =>
                        {
                            builder.WithOrigins("http://localhost:56003").AllowAnyHeader();
                        });
                });
            }    
    
    

    2、在configure方法中加入以下内容,重新编译运行就可以了。

            /// <summary>
            /// 
            /// </summary>
            /// <param name="app"></param>
            /// <param name="env"></param>
            // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
            public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
            {
                if (env.IsDevelopment())
                {
                    app.UseDeveloperExceptionPage();
                }
    
                app.UseRouting();
                
                app.UseCors("AllowSpecificOrigins");
                
                app.UseStaticFiles();
                app.UseAuthorization();
                app.UseEndpoints(endpoints =>
                {
                    endpoints.MapControllers();
                });
            }        
    

      

  • 相关阅读:
    Quartz2.0以上版本的单机和集群
    Mysql的Haproxy反向代理和负载均衡
    spring AOP原理解析
    Restful接口调用方法超详细总结
    mysql数据库主从同步
    数据备份的OSS接口
    读取properties配置文件的方法
    算法学习——堆排序(二叉树排序)
    回溯算法的实现
    冒泡排序
  • 原文地址:https://www.cnblogs.com/sjt072/p/11929084.html
Copyright © 2020-2023  润新知