需要做自定义授权管理 由于是 ResFul 各会导致很多API 的会重复 通过反射可以反射到API 上面模板进行区分 但是因为区分问题 采用验证方式就是把模板变成可解析的模板字符串 那么可以进行验证
获取 API
foreach (var item in typeInfos)
{
MethodInfo[] infoList = item.GetMethods();
foreach (MethodInfo methodsitem in infoList)
{
foreach (Attribute attribute in methodsitem.GetCustomAttributes())
{
if (attribute is HttpMethodAttribute)
{
HttpMethodAttribute apitype = (HttpMethodAttribute)attribute;
//Console.WriteLine(item.Name+"."+ apitype.Template);
// Console.WriteLine(apitype.HttpMethods);
// Console.WriteLine(apitype.Name+"---"+ apitype.Template+"---");
//apitype.TypeId.ToString().Replace("Microsoft.AspNetCore.Mvc.Http","").Replace("Attribute","")
string requestPath = apitype.TypeId.ToString().Replace("Microsoft.AspNetCore.Mvc.Http", "").Replace("Attribute", "");
string Path;
string Name;
if (string.IsNullOrEmpty(apitype.Template))
{
Path = requestPath + ":" + item.Name.Replace("Controller", "");
Name = requestPath + ":" + item.Name;
}
else
{
Path = requestPath + ":" + item.Name.Replace("Controller", "") + "/" + TempleValueConvter(apitype.Template);
Name = requestPath + ":" + item.Name + ":" + TempleValueConvter(apitype.Template);
}
unitOfWork.GetRepository<ApiResource>().Insert(new ApiResource
{
ResourceType = requestPath,
ResourcePath = Path,
ResourceTemple = apitype.Template,
ResourceName = Name,
});
}
}
}
}
解析API
private static string TempleValueConvter(string Temp)
{
StringBuilder stringBuilder = new StringBuilder();
string[] tempArr=Temp.Split("/");
for (int i = 0; i < tempArr.Length; i++)
{
if(i== tempArr.Length - 1)
{
stringBuilder.Append("{" + i + "}");
}
else
{
stringBuilder.Append("{" + i + "}" + "/");
}
}
return stringBuilder.ToString();
}
···