Skip to content

Commit 6464a49

Browse files
Expanded resolution of direct route controllers to include the "controller" and "actions" data tokens. Fixes #27 (#28)
1 parent 7a4178f commit 6464a49

File tree

2 files changed

+41
-4
lines changed

2 files changed

+41
-4
lines changed

src/Microsoft.AspNet.WebApi.Versioning/Dispatcher/ApiVersionControllerAggregator.cs

+37-4
Original file line numberDiff line numberDiff line change
@@ -104,13 +104,46 @@ private IEnumerable<HttpControllerDescriptor> EnumerateDirectRoutes()
104104
var template = subroute.Route.RouteTemplate;
105105
var comparer = StringComparer.OrdinalIgnoreCase;
106106
var controllers = from route in routes
107-
where comparer.Equals( route.RouteTemplate, template ) &&
108-
route.DataTokens.ContainsKey( "controller" )
109-
let controller = route.DataTokens["controller"] as HttpControllerDescriptor
110-
where controller != null
107+
where comparer.Equals( route.RouteTemplate, template )
108+
from controller in EnumerateControllersInDataTokens( route.DataTokens )
111109
select controller;
112110

113111
return controllers.Distinct();
114112
}
113+
114+
private static IEnumerable<HttpControllerDescriptor> EnumerateControllersInDataTokens( IDictionary<string, object> dataTokens )
115+
{
116+
Contract.Requires( dataTokens != null );
117+
Contract.Ensures( Contract.Result<IEnumerable<HttpControllerDescriptor>>() != null );
118+
119+
var value = default( object );
120+
121+
if ( dataTokens.TryGetValue( "controller", out value ) )
122+
{
123+
var controllerDescriptor = value as HttpControllerDescriptor;
124+
125+
if ( controllerDescriptor != null )
126+
{
127+
yield return controllerDescriptor;
128+
}
129+
130+
yield break;
131+
}
132+
133+
if ( dataTokens.TryGetValue( "actions", out value ) )
134+
{
135+
var actionDescriptors = value as HttpActionDescriptor[];
136+
137+
if ( actionDescriptors == null )
138+
{
139+
yield break;
140+
}
141+
142+
foreach ( var actionDescriptor in actionDescriptors )
143+
{
144+
yield return actionDescriptor.ControllerDescriptor;
145+
}
146+
}
147+
}
115148
}
116149
}

src/Microsoft.AspNet.WebApi.Versioning/Dispatcher/HttpControllerTypeCache.cs

+4
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,9 @@ private static string GetControllerName( Type type )
3232
var attribute = type.GetCustomAttributes<ControllerNameAttribute>( false ).SingleOrDefault();
3333

3434
if ( attribute != null )
35+
{
3536
return attribute.Name;
37+
}
3638

3739
// use standard convention for the controller name
3840
var name = type.Name;
@@ -75,7 +77,9 @@ internal ICollection<Type> GetControllerTypes( string controllerName )
7577
if ( cache.Value.TryGetValue( controllerName, out lookup ) )
7678
{
7779
foreach ( var grouping in lookup )
80+
{
7881
set.UnionWith( grouping );
82+
}
7983
}
8084

8185
return set;

0 commit comments

Comments
 (0)