C#获取类以及类下的方法(用于Asp.Net MVC)

  在开发MVC项目中遇到的问题,做权限控制时,通过MVC的过滤器来实现,所以在分配权限时希望获取到所有的控制器和Action方法,通过查找资料,参考了《Asp.Net MVC框架揭秘》,最终实现。

  在C#中,实现动态获取类和方法主要通过反射来实现,要引用System.Reflection。

 1     public ActionResult GetControllerAndAction()
 2         List<Type> controllerTypes = new List<Type>();    //创建控制器类型列表
 3      var assembly = Assembly.Load("MySoft.UI");    //加载程序集
 4      controllerTypes.AddRange(assembly.GetTypes().Where(type => typeof(IController).IsAssignableFrom(type) && type.Name!="ErrorController"));    //获取程序集下所有的类,通过Linq筛选继承IController类的所有类型
 5      StringBuilder jsonBuilder = new StringBuilder();    //创建动态字符串,拼接json数据    注:现在json类型传递数据比较流行,比xml简洁
 6      jsonBuilder.Append("[");
 7      foreach (var controller in controllerTypes)//遍历控制器类
 8      {
 9          jsonBuilder.Append("{\"controllerName\":\"");
10          jsonBuilder.Append(controller.Name);
11           jsonBuilder.Append("\",\"controllerDesc\":\"");
12           jsonBuilder.Append((controller.GetCustomAttribute(typeof(DescriptionAttribute)) as DescriptionAttribute)==null?"" : (controller.GetCustomAttribute(typeof(DescriptionAttribute)) as DescriptionAttribute).Description);    //获取对控制器的描述Description
13           jsonBuilder.Append("\",\"action\":[");
14           var actions = controller.GetMethods().Where(method => method.ReturnType.Name == "ActionResult");    //获取控制器下所有返回类型为ActionResult的方法,对MVC的权限控制只要限制所以的前后台交互请求就行,统一为ActionResult
15           foreach (var action in actions)
16           {
17               jsonBuilder.Append("{\"actionName\":\"");
18               jsonBuilder.Append(action.Name);
19               jsonBuilder.Append("\",\"actionDesc\":\"");
20               jsonBuilder.Append((action.GetCustomAttribute(typeof(DescriptionAttribute)) as DescriptionAttribute) == null ? "" : (action.GetCustomAttribute(typeof(DescriptionAttribute)) as DescriptionAttribute).Description);    //获取对Action的描述
21               jsonBuilder.Append("\"},");
22           }
23           jsonBuilder.Remove(jsonBuilder.Length - 1, 1);
24           jsonBuilder.Append("]},");
25       }
26       jsonBuilder.Remove(jsonBuilder.Length - 1, 1);
27       jsonBuilder.Append("]");
28       return Content(jsonBuilder.ToString(),"json/text");t"); 
时间: 2024-12-18 19:25:09

C#获取类以及类下的方法(用于Asp.Net MVC)的相关文章

python基本知识(八):定制类,双下划线方法

'''定制类: 1. 双下划线属性__attr__ 2. 元类metaclass''' # 综述 '''iterable/iterator:1. __iter__(): return iterable_obj 1) 实现了该方法的对象叫iterable 2) iter(obj)会调用该方法, 生成一个迭代器iterator 2. __next__(): 指明迭代器怎么返回值 1) next(iterator)会返回一个值, 直到所有的值都返回了报错StopIteration 2) for.. i

Creating Custom Helper Methods 创建自定义辅助器方法----辅助器方法 ------ 精通ASP.NET MVC 5

创建内联的辅助器方法 和 拓展方法 好像类似的功能. 不过写在前台更直观

兼容IE的头像上传插件的设计方法(asp.net mvc)

使用了两个插件 1.文件上传插件uploadify 2.图像编辑插件jquery.Jcrop 基于Asp.net的mvc设计模式,设计了该插件 下面贴代码: view(.cshtml): <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title></title> <link href="~/Bo

使用整体模型模板辅助器 Using Whole-Model Templated Helpers 模板辅助器方法 精通ASP.NET MVC 5

怎么会

Creating Form Elements --Using BeginForm and EndForm 使用内建的Form辅助器方法 精通ASP.NET MVC 5

Using the BeginForm and EndForm Helper Methods in the CreatePerson.cshtml File

ASP.NET MVC的帮助类HtmlHelper和UrlHelper

在ASP.NET MVC框架中没有了自己的控件,页面显示完全就回到了写html代码的年代.还好在asp.net mvc框架中也有自带的HtmlHelper和UrlHelper两个帮助类.另外在MvcContrib扩展项目中也有扩展一些帮助类,这样我们就不光只能使用完整的html来编写了需要显示的页面了,就可以使用这些帮助类来完成,但最后运行时都还是要生成html代码的. System.Web.Mvc.HtmlHelper学习及使用 先来看看HtmlHelper能帮我们生成一些什么样的html呢.

[转]ASP.NET MVC的帮助类HtmlHelper和UrlHelper

本文转自:http://www.cnblogs.com/greatandforever/archive/2010/04/20/1715914.html?login=1 在ASP.NET MVC框架中没有了自己的控件,页面显示完全就回到了写html代码的年代.还好在asp.net mvc框架中也有自带的HtmlHelper和UrlHelper两个帮助类.另外在MvcContrib扩展项目中也有扩展一些帮助类,这样我们就不光只能使用完整的html来编写了需要显示的页面了,就可以使用这些帮助类来完成,

[转]ASP.NET MVC 5 - 验证编辑方法(Edit method)和编辑视图(Edit view)

在本节中,您将验证电影控制器生成的编辑方法(Edit action methods)和视图.但是首先将修改点代码,使得发布日期属性(ReleaseDate)看上去更好.打开Models \ Movie.cs文件,并添加高亮行如下所示: using System; using System.ComponentModel.DataAnnotations; using System.Data.Entity; namespace MvcMovie.Models { public class Movie

ASP.NET MVC下的异步Action的定义和执行原理

Visual Studio提供的Controller创建向导默认为我们创建一个继承自抽象类Controller的Controller类型,这样的Controller只能定义同步Action方法.如果我们需要定义异步Action方法,必须继承抽象类AsyncController.这篇问你讲述两种不同的异步Action的定义方法和底层执行原理.[本文已经同步到<How ASP.NET MVC Works?>中] 目录 一.基于线程池的请求处理 二.两种异步Action方法的定义     XxxAs