跨域配置,时间格式序列化

master
lxw 7 months ago
parent 41c5e77097
commit 5ea3ebc9dc

@ -0,0 +1,27 @@
using System.Text.Json;
using System.Text.Json.Serialization;
namespace HybirdFrameworkCore.Entity;
/// <summary>
/// 序列化
/// </summary>
public class DatetimeJsonConverter : JsonConverter<DateTime>
{
public override DateTime Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
if (reader.TokenType == JsonTokenType.String)
{
if (DateTime.TryParse(reader.GetString(), out DateTime date))
return date;
}
return reader.GetDateTime();
}
public override void Write(Utf8JsonWriter writer, DateTime value, JsonSerializerOptions options)
{
writer.WriteStringValue(value.ToString("yyyy-MM-dd HH:mm:ss"));
}
}

@ -2,6 +2,7 @@ using Autofac;
using Autofac.Extensions.DependencyInjection;
using HybirdFrameworkCore.Autofac;
using HybirdFrameworkCore.Configuration;
using HybirdFrameworkCore.Entity;
using HybirdFrameworkCore.Redis;
using SqlSugar;
using SqlSugar.IOC;
@ -27,7 +28,25 @@ builder.Host.ConfigureContainer<ContainerBuilder>(cb =>
cb.RegisterModule(new AutofacModuleRegister());
});
//跨域
builder.Services.AddCors(options =>
{
options.AddPolicy
(name: "myCors",
builde =>
{
builde.WithOrigins("*", "*", "*")
.AllowAnyOrigin()
.AllowAnyHeader()
.AllowAnyMethod();
}
);
});
builder.Services.AddControllers().AddJsonOptions(configure =>
{
configure.JsonSerializerOptions.Converters.Add(new DatetimeJsonConverter());
});
//redis
var section = builder.Configuration.GetSection("Redis");
//连接字符串
@ -54,10 +73,11 @@ if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
app.UseStaticFiles();
}
app.UseAuthorization();
app.UseCors("myCors");
app.MapControllers();
AppInfo.Container = app.Services.GetAutofacRoot();

Loading…
Cancel
Save