本文中介绍的方式为采用mvc框架进行接口开发的时候,需要进行接口调试,但是目前没有十分合适和比较好的接口调试工具可以和mvc进行结合起来,所以需要自己编写接口调试工具,本文中介绍的就是遍历mvc遍历项目中的所有Controller里面的action及相关描述功能,当这个功能完成之后才能将相关的数据信息赋值到页面里面制作接口调试工具。
首先需要创建一个几个类
ReflectModel(Controller信息介绍类)
public class ReflectModel { /// <summary> /// Controller名称,通过反射Desction属性获得 /// </summary> public string ControllerDesction { set; get; } /// <summary> /// 控制器名称 /// </summary> public string ControllerName { set; get; } /// <summary> /// action介绍 /// </summary> public List<ReflectActionModel> ActionDesction { set; get; } }
ReflectActionModel(Action信息介绍类)
public class ReflectActionModel { /// 接口请求方式 /// </summary> public string ActionType { set; get; } /// <summary> /// 请求地址 /// </summary> public string ActionLink { set; get; } /// <summary> /// 接口请求的数据 /// </summary> public List<Data> ActionData{ set; get; } /// 接口名称 /// </summary> public string ActionName { set; get; } }
Data(接口参数信息类)
public class Data { /// <summary> /// 数据字段名 /// </summary> public string name { set; get; } /// <summary> /// 字段简介 /// </summary> public string des { set; get; } }
然后新建两个特性 DescriperAttribute(接口信息描述特性)和DataAttribute(接口参数信息描述特性)
public class DescriperAttribute:Attribute { /// 接口名称 /// </summary> public string ActionName { set; get; } /// 接口请求方式 /// </summary> public string ActionType { set; get; } }
// 比较重要,可以让特性进行重复使用 [AttributeUsageAttribute(AttributeTargets.All, AllowMultiple = true, Inherited = false)] public class DataAttribute:Attribute { /// <summary> /// 数据字段名 /// </summary> public string name { set; get; } /// <summary> /// 字段简介 /// </summary> public string des { set; get; } }
最后的实现方法
[Description("数据测试")] public class testController : Controller { // // GET: /test/ [HttpGet] [Descriper(ActionName = "测试", ActionType = "HttpGet")] [Data(name = "id", des = "用户id")] [Data(name = "name", des = "用户名称")] public JsonResult test999(int id, string name) { return Json("dsadsadasds"); } // GET: sysadmin/Lcgl [Descriper(ActionName = "查看", ActionType = "HttpGet")] [Data(name = "id", des = "用户id")] [Data(name = "name", des = "用户名称")] public ActionResult Index(int id,string name) { return View(); } [Descriper(ActionName = "新建", ActionType = "HttpPost")] public ActionResult Create(int id) { return View(); } /// <summary> /// 反射后台所有控制器和下面的公开action /// </summary> /// <returns></returns> [Descriper(ActionName = "获取Action", ActionType = "HttpPost")] [Data(name="id",des="用户id")] [Data(name = "name", des = "用户名称")] [Data(name = "price", des = "金额")] public JsonResult GetALLActionByReflection() { List<ReflectModel> controls = new List<ReflectModel>(); var asm = System.Reflection.Assembly.GetExecutingAssembly(); System.Collections.Generic.List<Type> typeList = new List<Type>(); var types = asm.GetTypes().Where(type => typeof(IController).IsAssignableFrom(type)); foreach (Type type in types) { string s = type.FullName.ToLower(); typeList.Add(type); } typeList.Sort(delegate(Type type1, Type type2) { return type1.FullName.CompareTo(type2.FullName); }); foreach (Type type in typeList) { ReflectModel rm = new ReflectModel(); rm.ActionDesction = new List<ReflectActionModel>(); System.Reflection.MemberInfo[] members = type.FindMembers(System.Reflection.MemberTypes.Method, System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.NonPublic | //【位屏蔽】 System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.DeclaredOnly, Type.FilterName, "*"); string controller = type.Name.Replace("Controller", ""); //反射Description属性,作为菜单名称 object[] descriptionList = type.GetCustomAttributes(typeof(DescriptionAttribute), false); if (descriptionList != null && descriptionList.Length > 0) { foreach (var dm in descriptionList) { DescriptionAttribute da = dm as DescriptionAttribute; rm.ControllerDesction = da.Description; break; } } else { rm.ControllerDesction = controller; } rm.ControllerName = controller; foreach (var m in members) { if (m.DeclaringType.Attributes.HasFlag(System.Reflection.TypeAttributes.Public) != true) continue; ReflectActionModel ram = new ReflectActionModel(); string action = m.Name; if (action.Contains("<") || action.Contains(">")) continue; //反射自定义属性DescriperAttribute,过滤不需要的 object[] deserlist = m.GetCustomAttributes(typeof(DescriperAttribute), false); if (deserlist != null && deserlist.Length > 0) { foreach (DescriperAttribute da in deserlist) { //DescriperAttribute da = cm as DescriperAttribute; //ram.ActionData = da.ActionData; ram.ActionName = da.ActionName; ram.ActionType = da.ActionType; break; } } ram.ActionLink = controller + "/" + action; //获取data参数信息 List<Data> DataList = new List<Data>(); object[] datalist = m.GetCustomAttributes(typeof(DataAttribute), false); if (datalist != null && datalist.Length > 0) { foreach (DataAttribute data in datalist) { Data da = new Data(); da.name = data.name; da.des = data.des; DataList.Add(da); } } ram.ActionData = DataList; rm.ActionDesction.Add(ram); } controls.Add(rm); } return Json(controls, JsonRequestBehavior.AllowGet); } }
最后实现结果