Browse Source

为 自动路由测试 提供了 方便

master
Nice 3 years ago
parent
commit
c324ba55fc
  1. 2
      Gear/Gear.csproj
  2. 61
      Gear/Gear/Abstractions/AppService.cs
  3. 87
      Gear/Gear/Application/ApplicationConvention.cs

2
Gear/Gear.csproj

@ -8,7 +8,7 @@
<Company>全网科技</Company>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<PackageId>Gear</PackageId>
<Version>0.1.5</Version>
<Version>0.1.7</Version>
<Product>Gear</Product>
<AssemblyName>Gear</AssemblyName>
</PropertyGroup>

61
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<IDecryptClaim>();
//获得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;
}
}
}

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

Loading…
Cancel
Save