asp.net core 3.1 解决跨域的问题

public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
readonly string MyAllowSpecificOrigins = “_myAllowSpecificOrigins”;
public IConfiguration Configuration { get; }

    // This method gets called by the runtime. Use this method to add services to the container.
    public void ConfigureServices(IServiceCollection services)
    {

//这里跨域
services.AddCors(options =>
{
options.AddPolicy(MyAllowSpecificOrigins,
builder => builder.WithOrigins(“http://192.168.1.167:8074”, “http://192.168.1.167:8095”)
.AllowAnyMethod()
.AllowAnyHeader()
.AllowCredentials());
});

        services.AddMvc().AddWebApiConventions();
        services.AddControllers();
        services.AddStackExchangeRedisCache(opsw =>
        {
            opsw.Configuration = "127.0.0.1:6379";//地址
            opsw.InstanceName = "Lw_";//标识

        });
        services.AddDbContext<fzcontext>(opt => opt.UseSqlServer(Configuration.GetConnectionString("gww")));
        services.AddSwaggerGen(c=>{
            c.SwaggerDoc("v1", new OpenApiInfo { Title = "My API", Version = "v1" });
            var xmlFile = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml";
            var xmlPath = Path.Combine(AppContext.BaseDirectory, xmlFile);
            c.IncludeXmlComments(xmlPath);
        });
    }

    // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
    public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
    {
  
        app.UseSwagger();
       // app.UseMiddleware<CorsMiddleware>();
        app.UseSwaggerUI(c =>
        {
            c.SwaggerEndpoint("/swagger/v1/swagger.json", "My API V1");
            c.RoutePrefix = string.Empty;
        });

app.UseRouting();
//一定要在中间
app.UseCors(MyAllowSpecificOrigins);
app.UseEndpoints(endpoints =>
{

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值