|
|
@ -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
|
|
|
|