Net Core WebApi ResFul 风格API 进行管理验证

需要做自定义授权管理 由于是 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();
        }
···
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值