Since areas are not easily supported in the web API (and also because I need more flexibility than the routing rules throughout the project), I use the attribute [RoutePrefix]on my controllers to group the web API controllers in namespaces, for example:
[RoutePrefix["Namespace1/Controller1"]
public class Controller1 : ApiControllerBase { }
[RoutePrefix["Namespace1/Controller2"]
public class Controller2 : ApiControllerBase { }
[RoutePrefix["Namespace1/Controller3"]
public class Controller3 : ApiControllerBase { }
[RoutePrefix["Namespace2/Controller4"]
public class Controller4 : ApiControllerBase { }
[RoutePrefix["Namespace2/Controller5"]
public class Controller5 : ApiControllerBase { }
[RoutePrefix["Namespace2/Controller6"]
public class Controller6 : ApiControllerBase { }
(They are in separate files and contain actions inside them, I just deleted this, along with the actual names, for simplicity.)
I am creating help documentation using web API help pages that work great. However, I would like to group and order the documentation for my "namespaces" (group by route prefix, and then sort alphabetically inside each).
, , . , Index.cshtml [ HelpPage, -API -]:
@foreach (IGrouping<HttpControllerDescriptor, ApiDescription> group in apiGroups)
{
@Html.DisplayFor(m => group, "ApiGroup")
}
:
@foreach (IGrouping<HttpControllerDescriptor, ApiDescription> group
in apiGroups.OrderBy(g => g.Key.GetCustomAttributes<RoutePrefixAttribute>().FirstOrDefault().Prefix)
.ThenBy(g => g.Key.ControllerName))
{
@Html.DisplayFor(m => group, "ApiGroup")
}
, : LINQ , g.Key.GetCustomAttributes<RoutePrefixAttribute>().FirstOrDefault() null . , ( ). ?