Browse Source

添加路由组成连字符转换配置

master
wanggang 1 year ago
parent
commit
a88baf226c
  1. 15
      code/src/Modules/BaseService/BaseService.Host/Startup.cs
  2. 25
      code/src/Modules/SettleAccount/host/SettleAccount.HttpApi.Host/Startup.cs

15
code/src/Modules/BaseService/BaseService.Host/Startup.cs

@ -1,7 +1,10 @@
using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc.ApplicationModels;
using Microsoft.AspNetCore.Routing;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using System.Text.RegularExpressions;
namespace BaseService namespace BaseService
{ {
@ -9,6 +12,8 @@ namespace BaseService
{ {
public void ConfigureServices(IServiceCollection services) 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<BaseServiceHostModule>(); services.AddApplication<BaseServiceHostModule>();
} }
@ -16,5 +21,15 @@ namespace BaseService
{ {
app.InitializeApplication(); 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();
}
}
} }
} }

25
code/src/Modules/SettleAccount/host/SettleAccount.HttpApi.Host/Startup.cs

@ -1,10 +1,11 @@
using System; using Microsoft.AspNetCore.Builder;
using System.Configuration;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc.ApplicationModels;
using Microsoft.AspNetCore.Routing;
using Microsoft.AspNetCore.Server.Kestrel.Core; using Microsoft.AspNetCore.Server.Kestrel.Core;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using System.Text.RegularExpressions;
namespace Win.Sfs.SettleAccount namespace Win.Sfs.SettleAccount
{ {
@ -12,6 +13,8 @@ namespace Win.Sfs.SettleAccount
{ {
public void ConfigureServices(IServiceCollection services) 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<SettleAccountHttpApiHostModule>(); services.AddApplication<SettleAccountHttpApiHostModule>();
services.Configure<KestrelServerOptions>(options => services.Configure<KestrelServerOptions>(options =>
{ {
@ -23,10 +26,7 @@ namespace Win.Sfs.SettleAccount
{ {
options.MaxRequestBodySize = 268435456; options.MaxRequestBodySize = 268435456;
options.AllowSynchronousIO = true; options.AllowSynchronousIO = true;
}); });
} }
public void Configure(IApplicationBuilder app, IWebHostEnvironment env, ILoggerFactory loggerFactory) public void Configure(IApplicationBuilder app, IWebHostEnvironment env, ILoggerFactory loggerFactory)
@ -34,8 +34,15 @@ namespace Win.Sfs.SettleAccount
app.InitializeApplication(); 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();
}
}
} }
} }
Loading…
Cancel
Save