var builder = WebApplication.CreateBuilder(args); // Add services to the container. builder.Services.AddCors(options => { options.AddPolicy("any", builder => { // 应用请求跨域 builder.SetIsOriginAllowed(_ => true).AllowAnyOrigin().AllowAnyMethod().AllowAnyHeader(); }); }); var app = builder.Build(); app.UseCors("AllowAll");
以上是应用跨域
=================================
var app = builder.Build(); app.UseCors("AllowAll"); app.UseStaticFiles(new StaticFileOptions { FileProvider = new PhysicalFileProvider(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "mp3")), HttpsCompression = Microsoft.AspNetCore.Http.Features.HttpsCompressionMode.Compress, RequestPath = "/mp3", OnPrepareResponse = (c) => { //静态资源跨域 c.Context.Response.Headers.Add("Access-Control-Allow-Origin", "*"); c.Context.Response.Headers.Add("Access-Control-Allow-Methods", "GET, POST, DELETE, PUT, OPTIONS, TRACE, HEAD, PATCH"); c.Context.Response.Headers.Add("Access-Control-Allow-Headers", "*"); } });
以上是静态资源跨域
标签:WebAPI,跨域,builder,Access,Headers,Add,net,app From: https://www.cnblogs.com/valeb/p/18090366