From 5ea3ebc9dcc6b61d1030cff9f0eec4ed6df0855b Mon Sep 17 00:00:00 2001 From: lxw Date: Thu, 16 May 2024 12:13:19 +0800 Subject: [PATCH] =?UTF-8?q?=E8=B7=A8=E5=9F=9F=E9=85=8D=E7=BD=AE=EF=BC=8C?= =?UTF-8?q?=E6=97=B6=E9=97=B4=E6=A0=BC=E5=BC=8F=E5=BA=8F=E5=88=97=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Entity/DatetimeJsonConverter.cs | 27 +++++++++++++++++++ WebStarter/Program.cs | 22 ++++++++++++++- 2 files changed, 48 insertions(+), 1 deletion(-) create mode 100644 HybirdFrameworkCore/Entity/DatetimeJsonConverter.cs diff --git a/HybirdFrameworkCore/Entity/DatetimeJsonConverter.cs b/HybirdFrameworkCore/Entity/DatetimeJsonConverter.cs new file mode 100644 index 0000000..4e0facd --- /dev/null +++ b/HybirdFrameworkCore/Entity/DatetimeJsonConverter.cs @@ -0,0 +1,27 @@ +using System.Text.Json; +using System.Text.Json.Serialization; + +namespace HybirdFrameworkCore.Entity; + + +/// +/// 序列化 +/// +public class DatetimeJsonConverter : JsonConverter +{ + 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")); + } +} \ No newline at end of file diff --git a/WebStarter/Program.cs b/WebStarter/Program.cs index 5cfa57f..0f9860c 100644 --- a/WebStarter/Program.cs +++ b/WebStarter/Program.cs @@ -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(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();