|
|
@ -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<SelectorModel> selectors)
|
|
|
|
//{
|
|
|
|