1.在使用 .net 6 开发WEBAPI程序时,出现跨域问题 。原来的写法不能用了。用下面的写法解决问题
builder.Services.AddCors(options =>
{
options.AddPolicy("any", builder =>
{
builder.SetIsOriginAllowed(_ => true).AllowAnyMethod().AllowAnyHeader().AllowCredentials();
});
}); 文章来源:https://www.toymoban.com/news/detail-601104.html
app.UseCors("any");文章来源地址https://www.toymoban.com/news/detail-601104.html
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<HostedService>();
//使用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>("/chatHub");
app.Run();
到了这里,关于asp.net core 6中跨域问题的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!