本文中介紹的方式為采用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);
}
}
最后實現結果

