Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 513371
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T07:24:57+00:00 2026-05-13T07:24:57+00:00

Is it possible to get all controllers available to a ControllerFactory? What I want

  • 0

Is it possible to get all controllers available to a ControllerFactory?
What I want to do is get a list of all controller types in application, but in a consistent way.

So that all controllers I get are the same ones default request resolution is using.

(The actual task is to find all action methods that have a given attribute).

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T07:24:57+00:00Added an answer on May 13, 2026 at 7:24 am

    You can use reflection to enumerate all classes in an assembly, and filter only classes inherit from Controller class.

    The best reference is asp.net mvc source code. Take a look of the implementations of ControllerTypeCache and ActionMethodSelector class.
    ControllerTypeCache shows how to get all controller classes available.

           internal static bool IsControllerType(Type t) {
                return
                    t != null &&
                    t.IsPublic &&
                    t.Name.EndsWith("Controller", StringComparison.OrdinalIgnoreCase) &&
                    !t.IsAbstract &&
                    typeof(IController).IsAssignableFrom(t);
            }
    
     public void EnsureInitialized(IBuildManager buildManager) {
                if (_cache == null) {
                    lock (_lockObj) {
                        if (_cache == null) {
                            List<Type> controllerTypes = GetAllControllerTypes(buildManager);
                            var groupedByName = controllerTypes.GroupBy(
                                t => t.Name.Substring(0, t.Name.Length - "Controller".Length),
                                StringComparer.OrdinalIgnoreCase);
                            _cache = groupedByName.ToDictionary(
                                g => g.Key,
                                g => g.ToLookup(t => t.Namespace ?? String.Empty, StringComparer.OrdinalIgnoreCase),
                                StringComparer.OrdinalIgnoreCase);
                        }
                    }
                }
            }
    

    And ActionMethodSelector shows how to check if a method has desired attribute.

    private static List<MethodInfo> RunSelectionFilters(ControllerContext controllerContext, List<MethodInfo> methodInfos) {
                // remove all methods which are opting out of this request
                // to opt out, at least one attribute defined on the method must return false
    
                List<MethodInfo> matchesWithSelectionAttributes = new List<MethodInfo>();
                List<MethodInfo> matchesWithoutSelectionAttributes = new List<MethodInfo>();
    
                foreach (MethodInfo methodInfo in methodInfos) {
                    ActionMethodSelectorAttribute[] attrs = (ActionMethodSelectorAttribute[])methodInfo.GetCustomAttributes(typeof(ActionMethodSelectorAttribute), true /* inherit */);
                    if (attrs.Length == 0) {
                        matchesWithoutSelectionAttributes.Add(methodInfo);
                    }
                    else if (attrs.All(attr => attr.IsValidForRequest(controllerContext, methodInfo))) {
                        matchesWithSelectionAttributes.Add(methodInfo);
                    }
                }
    
                // if a matching action method had a selection attribute, consider it more specific than a matching action method
                // without a selection attribute
                return (matchesWithSelectionAttributes.Count > 0) ? matchesWithSelectionAttributes : matchesWithoutSelectionAttributes;
            }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is it possible to get all/latest tweets from twitter public list? Example list: https://twitter.com/#!/thecaucus/u-s-senate
Is there a way to get all possible values from a MySQL enum column?
I would like to get all possible available currencies. Java 7 had provided such
Is it possible to get a list of all imported classes/namespaces in a PHP
Possible Duplicate: Get all files from VSS for a given date? I need to
Is it possible to get all 7 days for a given date using linq
Is it possible to get all div's within a page using jQuery including nested
In C#, is it possible to get all values of a particular column from
Possible Duplicate: How to get all of the IDs with jQuery? How can i
Possible Duplicate: Printing Table's structure/schema oracle has 'DESCRIBE' to get all the details of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.