Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You can use reflection to enumerate all classes in an assembly, and filter only classes inherit from Controller class.</p> <p>The best reference is <a href="http://aspnet.codeplex.com/Release/ProjectReleases.aspx?ReleaseId=24471" rel="noreferrer">asp.net mvc source code</a>. Take a look of the implementations of <a href="http://aspnet.codeplex.com/sourcecontrol/changeset/view/23011?projectName=aspnet#266456" rel="noreferrer">ControllerTypeCache</a> and <a href="http://aspnet.codeplex.com/sourcecontrol/changeset/view/23011?projectName=aspnet#266437" rel="noreferrer">ActionMethodSelector</a> class. ControllerTypeCache shows how to get all controller classes available. </p> <pre><code> internal static bool IsControllerType(Type t) { return t != null &amp;&amp; t.IsPublic &amp;&amp; t.Name.EndsWith("Controller", StringComparison.OrdinalIgnoreCase) &amp;&amp; !t.IsAbstract &amp;&amp; typeof(IController).IsAssignableFrom(t); } public void EnsureInitialized(IBuildManager buildManager) { if (_cache == null) { lock (_lockObj) { if (_cache == null) { List&lt;Type&gt; controllerTypes = GetAllControllerTypes(buildManager); var groupedByName = controllerTypes.GroupBy( t =&gt; t.Name.Substring(0, t.Name.Length - "Controller".Length), StringComparer.OrdinalIgnoreCase); _cache = groupedByName.ToDictionary( g =&gt; g.Key, g =&gt; g.ToLookup(t =&gt; t.Namespace ?? String.Empty, StringComparer.OrdinalIgnoreCase), StringComparer.OrdinalIgnoreCase); } } } } </code></pre> <p>And ActionMethodSelector shows how to check if a method has desired attribute.</p> <pre><code>private static List&lt;MethodInfo&gt; RunSelectionFilters(ControllerContext controllerContext, List&lt;MethodInfo&gt; 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&lt;MethodInfo&gt; matchesWithSelectionAttributes = new List&lt;MethodInfo&gt;(); List&lt;MethodInfo&gt; matchesWithoutSelectionAttributes = new List&lt;MethodInfo&gt;(); 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 =&gt; 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 &gt; 0) ? matchesWithSelectionAttributes : matchesWithoutSelectionAttributes; } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload