ASP.NET Core 跨域问题

当前后台分离的时候,请求net core就会出现跨域的问题,405(Method Not Allowed)

解决:

在StartUpConfigureServices添加如下代码

//appsettings.json中配置

//"CorsUrls": //"http://localhost:8081,http://localhost:8080,http://127.0.0.1:8081,http://127.0.0.1:8//080,http://132.232.2.109,http://www.volcore.xyz",

string corsUrls = Configuration["CorsUrls"];

if (string.IsNullOrEmpty(corsUrls))

{

      throw new Exception("请配置跨请求的前端Url");

}

services.AddCors(options =>

{

options.AddDefaultPolicy(

builder =>

{

       builder.WithOrigins(corsUrls.Split(","))

       //添加预检请求过期时间

      .SetPreflightMaxAge(TimeSpan.FromSeconds(2520))

       .AllowCredentials()

        .AllowAnyHeader().AllowAnyMethod();

});

});

Configure方法中添加 app.UseCors();

参考

https://www.cnblogs.com/stulzq/p/9392150.html

原文地址:https://www.cnblogs.com/zxking/p/13439460.html