MVC 获取所有操作方法

发布于 2024-11-03 17:36:43 字数 36 浏览 1 评论 0原文

有没有办法获取我的 MVC 3 项目的所有操作方法的列表?

Is there a way to get a list of all Actions Methods of my MVC 3 project?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

沉默的熊 2024-11-10 17:36:43

这将为您提供一个字典,其中控制器类型作为键,其 MethodInfos 的 IEnumerable 作为值。

        var assemblies = AppDomain.CurrentDomain.GetAssemblies(); // currently loaded assemblies
        var controllerTypes = assemblies
            .SelectMany(a => a.GetTypes())
            .Where(t => t != null
                && t.IsPublic // public controllers only
                && t.Name.EndsWith("Controller", StringComparison.OrdinalIgnoreCase) // enfore naming convention
                && !t.IsAbstract // no abstract controllers
                && typeof(IController).IsAssignableFrom(t)); // should implement IController (happens automatically when you extend Controller)
        var controllerMethods = controllerTypes.ToDictionary(
            controllerType => controllerType,
            controllerType => controllerType.GetMethods(BindingFlags.Public | BindingFlags.Instance).Where(m => typeof(ActionResult).IsAssignableFrom(m.ReturnType)));

它不仅仅查找当前程序集,还会返回方法,例如返回 JsonResult 而不是 ActionResult。 (JsonResult 实际上继承自 ActionResult)

编辑:对于 Web API 支持

更改

&& typeof(IController).IsAssignableFrom(t)); // should implement IController (happens automatically when you extend Controller)

&& typeof(IHttpController).IsAssignableFrom(t)); // should implement IHttpController (happens automatically when you extend ApiController)

并删除此:

.Where(m => typeof(ActionResult).IsAssignableFrom(m.ReturnType))

因为 Web API 方法几乎可以返回任何内容。 (POCO,HttpResponseMessage,...)

This will give you a dictionary with the controller type as key and an IEnumerable of its MethodInfos as value.

        var assemblies = AppDomain.CurrentDomain.GetAssemblies(); // currently loaded assemblies
        var controllerTypes = assemblies
            .SelectMany(a => a.GetTypes())
            .Where(t => t != null
                && t.IsPublic // public controllers only
                && t.Name.EndsWith("Controller", StringComparison.OrdinalIgnoreCase) // enfore naming convention
                && !t.IsAbstract // no abstract controllers
                && typeof(IController).IsAssignableFrom(t)); // should implement IController (happens automatically when you extend Controller)
        var controllerMethods = controllerTypes.ToDictionary(
            controllerType => controllerType,
            controllerType => controllerType.GetMethods(BindingFlags.Public | BindingFlags.Instance).Where(m => typeof(ActionResult).IsAssignableFrom(m.ReturnType)));

It looks in more than just the current assembly and it will also return methods that, for example, return JsonResult instead of ActionResult. (JsonResult actually inherits from ActionResult)

Edit: For Web API support

Change

&& typeof(IController).IsAssignableFrom(t)); // should implement IController (happens automatically when you extend Controller)

to

&& typeof(IHttpController).IsAssignableFrom(t)); // should implement IHttpController (happens automatically when you extend ApiController)

and remove this:

.Where(m => typeof(ActionResult).IsAssignableFrom(m.ReturnType))

Because Web API methods can return just about anything. (POCO's, HttpResponseMessage, ...)

花桑 2024-11-10 17:36:43

您可以使用它在运行时反映程序集,以在返回 ActionResult 的控制器中生成方法列表:

    public IEnumerable<MethodInfo> GetMvcActionMethods()
    {
        return
            Directory.GetFiles(Assembly.GetExecutingAssembly().Location)
                .Select(Assembly.LoadFile)
                .SelectMany(
                    assembly =>
                    assembly.GetTypes()
                            .Where(t => typeof (Controller).IsAssignableFrom(t))
                            .SelectMany(type => (from action in type.GetMethods(BindingFlags.Public | BindingFlags.Instance) 
                                                 where action.ReturnType == typeof(ActionResult) 
                                                 select action)
                                        )
                    );
    }

这将为您提供操作,但不会提供视图列表(即,如果您可以在每个动作)

You can use this to reflect over the assemblies at runtime to produce a list of methods in Controllers that return ActionResult:

    public IEnumerable<MethodInfo> GetMvcActionMethods()
    {
        return
            Directory.GetFiles(Assembly.GetExecutingAssembly().Location)
                .Select(Assembly.LoadFile)
                .SelectMany(
                    assembly =>
                    assembly.GetTypes()
                            .Where(t => typeof (Controller).IsAssignableFrom(t))
                            .SelectMany(type => (from action in type.GetMethods(BindingFlags.Public | BindingFlags.Instance) 
                                                 where action.ReturnType == typeof(ActionResult) 
                                                 select action)
                                        )
                    );
    }

This will give you the actions, but not the list of Views (i.e. it won't work if you can use different views in each action)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文