Browse Source

Bug修复

master
Nice 3 years ago
parent
commit
47f8542396
  1. 47
      Gear/Gear/Application/ApplicationConvention.cs
  2. 5
      Gear/Gear/Infrastructure/Extensions/ServiceCollectionServiceExtensions.cs

47
Gear/Gear/Application/ApplicationConvention.cs

@ -47,10 +47,7 @@ namespace Gear.Application
{ {
foreach (var action in controller.Actions) foreach (var action in controller.Actions)
{ {
action.Selectors[0].AttributeRouteModel = new(new RouteAttribute( action.Selectors[0].AttributeRouteModel = new(new RouteAttribute(RouteTemplate(action)));
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) })); 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 //清空Selectors

5
Gear/Gear/Infrastructure/Extensions/ServiceCollectionServiceExtensions.cs

@ -83,7 +83,8 @@ namespace Gear.Infrastructure.Extensions
typeof(Use).IsAssignableFrom(o))) typeof(Use).IsAssignableFrom(o)))
{ {
var interfaceType = type.GetInterfaces().FirstOrDefault(interfaceType => interfaceType.Name.EndsWith(type.Name)); var interfaceType = type.GetInterfaces().FirstOrDefault(interfaceType => interfaceType.Name.EndsWith(type.Name));
if (interfaceType != null)
{
services.TryAddTransient(type, type); services.TryAddTransient(type, type);
services.TryAddTransient(interfaceType, o => services.TryAddTransient(interfaceType, o =>
{ {
@ -92,6 +93,8 @@ namespace Gear.Infrastructure.Extensions
return use; return use;
}); });
} }
}
} }
private static void AddScopedDependency(this IServiceCollection services, Type[] types) private static void AddScopedDependency(this IServiceCollection services, Type[] types)

Loading…
Cancel
Save