diff --git a/Gear/Gear.csproj b/Gear/Gear.csproj
index 0acac2d..8cc7409 100644
--- a/Gear/Gear.csproj
+++ b/Gear/Gear.csproj
@@ -8,7 +8,7 @@
全网科技
true
Gear
- 0.1.5
+ 0.1.7
Gear
Gear
diff --git a/Gear/Gear/Abstractions/AppService.cs b/Gear/Gear/Abstractions/AppService.cs
index f51083c..dcfe81e 100644
--- a/Gear/Gear/Abstractions/AppService.cs
+++ b/Gear/Gear/Abstractions/AppService.cs
@@ -1,5 +1,8 @@
using Microsoft.AspNetCore.Http;
using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Reflection;
using System.Security.Claims;
namespace Gear.Abstractions
@@ -34,7 +37,63 @@ namespace Gear.Abstractions
protected IDecryptClaim DecryptClaim => LazyServiceProvider.LazyGetService();
+ //获得HttpMethod
+ public static string GetHttpMethod(string actionName)
+ {
+ if (actionName.StartsWith("Get"))
+ return "GET";
+
+ if (actionName.StartsWith("Put") || actionName.StartsWith("Update"))
+ return "PUT";
+
+ if (actionName.StartsWith("Delete") || actionName.StartsWith("Remove"))
+ return "DELETE";
+
+ if (actionName.StartsWith("Patch"))
+ return "PATCH";
+
+ return "POST";
+ }
+
+ //获得一个自定义的路由
+ public static string RouteTemplate(string controllerTypeName, string actionMethodName, string idStr)
+ {
+ string url = "api";
+
+ //控制器部分
+ url += $"/{FiterControllerName(controllerTypeName)}";
+
+ //方法部分
+ string actionName = FiterActionName(actionMethodName);
+ if (!string.IsNullOrEmpty(actionName))
+ {
+ url += $"/{actionName}";
+ }
+
+ // id 部分
+ if (!string.IsNullOrEmpty(idStr))
+ {
+ url += $"/{idStr}";
+ }
+
+ return url;
+ }
+ // 过滤控制器
+ public static string FiterControllerName(string controllerName)
+ {
+ foreach (var commonPostfixe in CommonPostfixes)
+ controllerName = controllerName.Replace(commonPostfixe, "");
+ return controllerName;
+ }
+ // 过滤方法
+ public static string FiterActionName(string actionName)
+ {
+ //对外方法不能有这些关键字
+ foreach (var old in ActionPostfixes)
+ actionName = actionName.Replace(old, "");
+ return actionName;
+ }
protected long GetUserId()
{
if ((Configuration["LocalDebug"] ?? "") == "true")
@@ -49,7 +108,7 @@ namespace Gear.Abstractions
return result;
}
-
+
}
}
diff --git a/Gear/Gear/Application/ApplicationConvention.cs b/Gear/Gear/Application/ApplicationConvention.cs
index 632c243..77d43d7 100644
--- a/Gear/Gear/Application/ApplicationConvention.cs
+++ b/Gear/Gear/Application/ApplicationConvention.cs
@@ -17,7 +17,7 @@ namespace Gear.Application
{
application.Controllers.ToList().ForEach(controller =>
{
- controller.ControllerName = FiterControllerName(controller.ControllerName);
+ controller.ControllerName = AppService.FiterControllerName(controller.ControllerName);
ConfigureSelector(controller);
ConfigureParameters(controller);
ConfigureApiExplorer(controller);
@@ -47,8 +47,11 @@ namespace Gear.Application
{
foreach (var action in controller.Actions)
{
- action.Selectors[0].AttributeRouteModel = new(new RouteAttribute(RouteTemplate(action)));
- action.Selectors[0].ActionConstraints.Add(new HttpMethodActionConstraint(new[] { GetHttpMethod(action) }));
+ 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].ActionConstraints.Add(new HttpMethodActionConstraint(new[] { AppService.GetHttpMethod(action.ActionName) }));
}
}
//配置方法参数
@@ -79,85 +82,7 @@ namespace Gear.Application
- //获得一个自定义的路由
- private static string RouteTemplate(ActionModel action)
- {
- string url = "api";
-
- //控制器部分
- url += $"/{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 = 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;
- }
-
- //获得HttpMethod
- private static string GetHttpMethod(ActionModel action)
- {
- string actionName = action.ActionName;
-
- if (actionName.StartsWith("Get"))
- return "GET";
-
- if (actionName.StartsWith("Put") || actionName.StartsWith("Update"))
- return "PUT";
-
- if (actionName.StartsWith("Delete") || actionName.StartsWith("Remove"))
- return "DELETE";
- if (actionName.StartsWith("Patch"))
- return "PATCH";
-
- return "POST";
- }
-
-
- // 过滤控制器
- private static string FiterControllerName(string controllerName)
- {
- foreach (var commonPostfixe in AppService.CommonPostfixes)
- controllerName = controllerName.Replace(commonPostfixe, "");
- return controllerName;
- }
- // 过滤方法
- private static string FiterActionName(string actionName)
- {
- //对外方法不能有这些关键字
- foreach (var old in AppService.ActionPostfixes)
- actionName = actionName.Replace(old, "");
- return actionName;
- }
//清空Selectors
//private static void RemoveSelectors(IList selectors)
//{