From a88baf226c53d304735b8345c000fd903936950d Mon Sep 17 00:00:00 2001 From: wanggang <76527413@qq.com> Date: Thu, 13 Jul 2023 09:36:42 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E8=B7=AF=E7=94=B1=E7=BB=84?= =?UTF-8?q?=E6=88=90=E8=BF=9E=E5=AD=97=E7=AC=A6=E8=BD=AC=E6=8D=A2=E9=85=8D?= =?UTF-8?q?=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../BaseService/BaseService.Host/Startup.cs | 15 +++++++++++ .../SettleAccount.HttpApi.Host/Startup.cs | 27 ++++++++++++------- 2 files changed, 32 insertions(+), 10 deletions(-) diff --git a/code/src/Modules/BaseService/BaseService.Host/Startup.cs b/code/src/Modules/BaseService/BaseService.Host/Startup.cs index 79a41d59..9b7e1ca5 100644 --- a/code/src/Modules/BaseService/BaseService.Host/Startup.cs +++ b/code/src/Modules/BaseService/BaseService.Host/Startup.cs @@ -1,7 +1,10 @@ using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; +using Microsoft.AspNetCore.Mvc.ApplicationModels; +using Microsoft.AspNetCore.Routing; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; +using System.Text.RegularExpressions; namespace BaseService { @@ -9,6 +12,8 @@ namespace BaseService { public void ConfigureServices(IServiceCollection services) { + services.AddRouting(options => options.ConstraintMap["slugify"] = typeof(SlugifyParameterTransformer)); + services.AddMvc(options => options.Conventions.Add(new RouteTokenTransformerConvention(new SlugifyParameterTransformer()))); services.AddApplication(); } @@ -16,5 +21,15 @@ namespace BaseService { app.InitializeApplication(); } + public class SlugifyParameterTransformer : IOutboundParameterTransformer + { + public string TransformOutbound(object value) + { + if (value == null) { return null; } + var str = value.ToString(); + if (string.IsNullOrEmpty(str)) { return null; } + return Regex.Replace(str?.ToString(), "([a-z])([A-Z])", "$1-$2").ToLowerInvariant(); + } + } } } diff --git a/code/src/Modules/SettleAccount/host/SettleAccount.HttpApi.Host/Startup.cs b/code/src/Modules/SettleAccount/host/SettleAccount.HttpApi.Host/Startup.cs index 054a61df..8daf20ee 100644 --- a/code/src/Modules/SettleAccount/host/SettleAccount.HttpApi.Host/Startup.cs +++ b/code/src/Modules/SettleAccount/host/SettleAccount.HttpApi.Host/Startup.cs @@ -1,10 +1,11 @@ -using System; -using System.Configuration; -using Microsoft.AspNetCore.Builder; +using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; +using Microsoft.AspNetCore.Mvc.ApplicationModels; +using Microsoft.AspNetCore.Routing; using Microsoft.AspNetCore.Server.Kestrel.Core; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; +using System.Text.RegularExpressions; namespace Win.Sfs.SettleAccount { @@ -12,6 +13,8 @@ namespace Win.Sfs.SettleAccount { public void ConfigureServices(IServiceCollection services) { + services.AddRouting(options => options.ConstraintMap["slugify"] = typeof(SlugifyParameterTransformer)); + services.AddMvc(options=>options.Conventions.Add(new RouteTokenTransformerConvention(new SlugifyParameterTransformer()))); services.AddApplication(); services.Configure(options => { @@ -23,10 +26,7 @@ namespace Win.Sfs.SettleAccount { options.MaxRequestBodySize = 268435456; options.AllowSynchronousIO = true; - - }); - } public void Configure(IApplicationBuilder app, IWebHostEnvironment env, ILoggerFactory loggerFactory) @@ -34,8 +34,15 @@ namespace Win.Sfs.SettleAccount app.InitializeApplication(); } - - - + public class SlugifyParameterTransformer : IOutboundParameterTransformer + { + public string TransformOutbound(object value) + { + if (value == null) { return null; } + var str = value.ToString(); + if (string.IsNullOrEmpty(str)) { return null; } + return Regex.Replace(str?.ToString(), "([a-z])([A-Z])", "$1-$2").ToLowerInvariant(); + } + } } -} +} \ No newline at end of file