asp.net core 6中跨域问题

1.在使用 .net 6 开发WEBAPI程序时,出现跨域问题 。原来的写法不能用了。用下面的写法解决问题

builder.Services.AddCors(options =>
{
    options.AddPolicy("any", builder =>
    {
        builder.SetIsOriginAllowed(_ => true).AllowAnyMethod().AllowAnyHeader().AllowCredentials();
    });
}); 

 app.UseCors("any");



using ServiceStack;

string Dir1 = DateTime.Now.ToString("yyyy-MM-dd");
string Dir2 = DateTime.Now.ToString("yyyy-MM-dd HH");
//string s = LogEvent.Properties.GetValueOrDefault("SourceContext").ToString();

//LogEvent.
Log.Logger = new LoggerConfiguration()
	.MinimumLevel.Debug()
	.MinimumLevel.Override("Microsoft", LogEventLevel.Information)
	.ReadFrom.Configuration(new ConfigurationBuilder()
	.AddJsonFile("appsettings.json")
	.Build())
	.Enrich.FromLogContext()
					.WriteTo.Elasticsearch(new ElasticsearchSinkOptions(new Uri("http://192.168.1.104:9200/"))
					{
						BufferBaseFilename = "./logs/buffer",
						IndexFormat = "StraightLineSorting-{0:yyyy.MM}",
						EmitEventFailure = EmitEventFailureHandling.RaiseCallback,
						FailureCallback = e => Debug.WriteLine($"unable to ------{e.MessageTemplate}"),
						AutoRegisterTemplate = true,
						DetectElasticsearchVersion = true,

						OverwriteTemplate = true,
						// TemplateName = yourTemplateName,
						AutoRegisterTemplateVersion = AutoRegisterTemplateVersion.ESv7,
						TypeName = null,
						BatchAction = ElasticOpType.Create
						// AutoRegisterTemplateVersion = AutoRegisterTemplateVersion.ESv7, 
						//InlineFields = true,
						//IndexDecider = (@event, offset) => "MyES",
						//CustomFormatter = new RenderedCompactJsonFormatter()
					})
	//  .WriteTo.Async(c => c.File(Path.Combine(AppDomain.CurrentDomain.BaseDirectory + $"/Slog/{Dir1}/{Dir2}{typeof(LevelAlias).FullName}/", $"log.txt"), rollingInterval: RollingInterval.Hour))
	.WriteTo.Async(c => c.Console(new JsonFormatter()))
	.WriteTo.Async(c => c.File(Path.Combine(AppDomain.CurrentDomain.BaseDirectory + $"/Slog/{Dir1}/{Dir2}/Debug/", $"log.txt"), restrictedToMinimumLevel: LogEventLevel.Debug, rollingInterval: RollingInterval.Hour))
	.WriteTo.Async(c => c.File(Path.Combine(AppDomain.CurrentDomain.BaseDirectory + $"/Slog/{Dir1}/{Dir2}/Info/", $"log.txt"), restrictedToMinimumLevel: LogEventLevel.Information, rollingInterval: RollingInterval.Hour))
	.WriteTo.Async(c => c.File(Path.Combine(AppDomain.CurrentDomain.BaseDirectory + $"/Slog/{Dir1}/{Dir2}/Warning/", $"log.txt"), restrictedToMinimumLevel: LogEventLevel.Warning, rollingInterval: RollingInterval.Hour))
	.WriteTo.Async(c => c.File(Path.Combine(AppDomain.CurrentDomain.BaseDirectory + $"/Slog/{Dir1}/{Dir2}/Error/", $"log.txt"), restrictedToMinimumLevel: LogEventLevel.Error, rollingInterval: RollingInterval.Hour))
	// .WriteTo.Async(path: "error.log", restrictedToMinimumLevel: LogEventLevel.Error, rollingInterval: RollingInterval.Day)
	// .WriteTo.Elasticsearch(new ElasticsearchSinkOptions(new Uri("http://192.168.1.104:9200/MyES")))
	.CreateLogger();


//global using StraightLineSorting.Helper;
var builder = WebApplication.CreateBuilder(args);

// Add services to the container.
builder.Services.AddControllersWithViews();

 

builder.Services.AddCors(options =>
{
	options.AddPolicy("any", builder =>
	{
		builder.SetIsOriginAllowed(_ => true).AllowAnyMethod().AllowAnyHeader().AllowCredentials();
	});
}); 

builder.Services.AddSignalR();
builder.Services.AddHostedService();
 
//使用Serilog日志记录
builder.Host.UseSerilog();

 

var app = builder.Build();

// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
{
	app.UseExceptionHandler("/Home/Error");
}
app.UseStatusCodePagesWithRedirects("/Error/{0}");

app.UseStaticFiles();

app.UseRouting();

app.UseAuthorization();

app.MapControllerRoute(
	name: "default",
	pattern: "{controller=Login}/{action=Login}/{id?}");

app.UseCors("any");
 

app.MapHub("/chatHub");

 

app.Run();
 

你可能感兴趣的:(WEBAPI,asp.net,elasticsearch,后端)