在ASP.NET MVC开发中,通常有很多Controller,Action.我们可以从外面以反射,Linq的语法来获取些信息.Linq语法使得代码可读性高.
看UnitTest:
/// <summary> /// Tests the get info from controller. /// </summary> /// <remarks>http://wintersun.cnblogs.com </remarks> [TestMethod] public void TestGetInfoFromController() { var controllers = from t in GetAllControllerTypes() where typeof(Controller).IsAssignableFrom(t) && !t.IsAbstract orderby t.FullName from m in t.GetMethods(BindingFlags.Instance | BindingFlags.Public | BindingFlags.DeclaredOnly) where !m.IsSpecialName select new { ControllerName = FormatControllerName(t.FullName), ActionName = m.Name, Params = m.GetParameters() }; controllers.ToList().ForEach(c => Debug.WriteLine(string.Format("Controller: {0}, Action: {1}({2})", c.ControllerName, c.ActionName, string.Join(", ", c.Params.Select(p => p.Name). ToArray())))); Debug.WriteLine(string.Format("Controller/action count: {0}", controllers.Count())); Debug.WriteLine(string.Format("Controller count: {0}", controllers.GroupBy(c => c.ControllerName).Count())); } /// <summary> /// Gets all controller types. /// </summary> /// <returns>all types in an assembly where my controllers can be found</returns> private static Type[] GetAllControllerTypes() { return typeof(ProductManageController).Assembly.GetTypes(); } /// <summary> /// Formats the name of the controller,remove all of the namespace information from the controller names /// </summary> /// <param name="typeName">Name of the type.</param> /// <returns></returns> private static string FormatControllerName(string typeName) { return typeName.Replace("Demo1Web.", string.Empty).Replace("Controllers.", string.Empty); }
Controller: HomeController, Action: Index()
Controller: HomeController, Action: test()
Controller: HomeController, Action: ThisActionHasProblem()
Controller: HomeController, Action: Category(form)
Controller: ProductManageController, Action: Delete(ProductId)
Controller: ProductManageController, Action: DeleteSome(form)
Controller: ProductManageController, Action: EditProduct(Id)
Controller: ProductManageController, Action: ProductList(id)
Controller: ProductManageController, Action: QueryAllProducts(id, form)
Controller: ProductManageController, Action: SaveByBinder(productId, product)
Controller: ProductManageController, Action: Save(ProductId, form)
Controller: ProductManageController, Action: ViewProduct(Id)
Controller/action count: 12
Controller count: 2
1 passed, 0 failed, 0 skipped, took 0.97 seconds (NUnit_VSTS).
清晰的列表,有趣吧.
Author: Petter Liu http://wintersun.cnblogs.com
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· [.NET]调用本地 Deepseek 模型
· 一个费力不讨好的项目,让我损失了近一半的绩效!
· .NET Core 托管堆内存泄露/CPU异常的常见思路
· PostgreSQL 和 SQL Server 在统计信息维护中的关键差异
· C++代码改造为UTF-8编码问题的总结
· 实操Deepseek接入个人知识库
· CSnakes vs Python.NET:高效嵌入与灵活互通的跨语言方案对比
· 【.NET】调用本地 Deepseek 模型
· Plotly.NET 一个为 .NET 打造的强大开源交互式图表库
· 上周热点回顾(2.17-2.23)