From 47f8542396c1da3ab57e6a10d460e48aee014ce4 Mon Sep 17 00:00:00 2001 From: Nice <2017875139@qq.com> Date: Thu, 30 Sep 2021 14:27:04 +0800 Subject: [PATCH] =?UTF-8?q?Bug=E4=BF=AE=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Gear/Application/ApplicationConvention.cs | 47 +++++++++++++++++-- .../ServiceCollectionServiceExtensions.cs | 17 ++++--- 2 files changed, 53 insertions(+), 11 deletions(-) diff --git a/Gear/Gear/Application/ApplicationConvention.cs b/Gear/Gear/Application/ApplicationConvention.cs index 77d43d7..8bfff8b 100644 --- a/Gear/Gear/Application/ApplicationConvention.cs +++ b/Gear/Gear/Application/ApplicationConvention.cs @@ -47,10 +47,7 @@ namespace Gear.Application { foreach (var action in controller.Actions) { - action.Selectors[0].AttributeRouteModel = new(new RouteAttribute( - AppService.RouteTemplate(action.Controller.ControllerType.Name, - action.ActionMethod.Name, - action.Parameters.Count == 1 && action.Parameters.Any(p => p.Name.EndsWith("Id", StringComparison.Ordinal)) ? "Id" : ""))); + action.Selectors[0].AttributeRouteModel = new(new RouteAttribute(RouteTemplate(action))); action.Selectors[0].ActionConstraints.Add(new HttpMethodActionConstraint(new[] { AppService.GetHttpMethod(action.ActionName) })); } } @@ -81,6 +78,48 @@ namespace Gear.Application } + //获得一个自定义的路由 + private static string RouteTemplate(ActionModel action) + { + string url = "api"; + + //控制器部分 + url += $"/{AppService.FiterControllerName(action.Controller.ControllerType.Name)}"; + + // id 部分 + var idParameterModel = action.Parameters.FirstOrDefault(p => p.ParameterName == "id"); + if (idParameterModel != null) + { + if (action.Parameters.Any(temp => temp.ParameterName == "id")) + url += "/{id}"; + else + { + var properties = idParameterModel.ParameterType.GetProperties(BindingFlags.Instance | BindingFlags.Public); + + foreach (var property in properties) + { + url += "/{" + property.Name + "}"; + } + } + } + + //方法部分 + string actionName = AppService.FiterActionName(action.ActionMethod.Name); + if (!string.IsNullOrEmpty(actionName)) + { + url += $"/{actionName}"; + + // id 部分 + var secondaryIds = action.Parameters.Where(p => p.ParameterName.EndsWith("Id", StringComparison.Ordinal)).ToList(); + if (secondaryIds.Count == 1) + { + url += $"/{{{secondaryIds[0].Name}}}"; + } + + } + + return url; + } //清空Selectors diff --git a/Gear/Gear/Infrastructure/Extensions/ServiceCollectionServiceExtensions.cs b/Gear/Gear/Infrastructure/Extensions/ServiceCollectionServiceExtensions.cs index ef80c04..aca647a 100644 --- a/Gear/Gear/Infrastructure/Extensions/ServiceCollectionServiceExtensions.cs +++ b/Gear/Gear/Infrastructure/Extensions/ServiceCollectionServiceExtensions.cs @@ -83,14 +83,17 @@ namespace Gear.Infrastructure.Extensions typeof(Use).IsAssignableFrom(o))) { var interfaceType = type.GetInterfaces().FirstOrDefault(interfaceType => interfaceType.Name.EndsWith(type.Name)); - - services.TryAddTransient(type, type); - services.TryAddTransient(interfaceType, o => + if (interfaceType != null) { - Use use = (Use)o.GetService(type); - use.LazyServiceProvider = o.GetService(); - return use; - }); + services.TryAddTransient(type, type); + services.TryAddTransient(interfaceType, o => + { + Use use = (Use)o.GetService(type); + use.LazyServiceProvider = o.GetService(); + return use; + }); + } + } }