概述
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<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中跨域问题的全部内容,希望文章能够帮你解决asp.net core 6中跨域问题所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复