Asp.net web api Web API帮助页-按路由前缀排序控制器

Asp.net web api Web API帮助页-按路由前缀排序控制器,asp.net-web-api,asp.net-web-api-routing,asp.net-web-api-helppages,Asp.net Web Api,Asp.net Web Api Routing,Asp.net Web Api Helppages,由于在Web API中不容易支持区域(也因为我需要比项目范围的路由规则更大的灵活性),我正在使用控制器上的[RoutePrefix]属性将Web API控制器分组到名称空间中,例如: [RoutePrefix["Namespace1/Controller1"] public class Controller1 : ApiControllerBase { } [RoutePrefix["Namespace1/Controller2"] public class Controller2 : Api

由于在Web API中不容易支持区域(也因为我需要比项目范围的路由规则更大的灵活性),我正在使用控制器上的
[RoutePrefix]
属性将Web API控制器分组到名称空间中,例如:

[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 { }
(这些文件位于单独的文件中,其中包含操作,为了简单起见,我刚刚删除了它们以及实际名称。)

我使用WebAPI帮助页面生成帮助文档,效果很好。但是,我希望按照我的“名称空间”(按路由前缀分组,然后在每个名称空间中按字母顺序排序)对文档进行分组和排序

我决定一开始只进行排序,然后在排序开始后计算分组。为了使排序正常工作,我尝试从以下内容更改我的
Index.cshtml
[在Web API帮助页面Nuget软件包创建的
HelpPage
区域中]:

@foreach (IGrouping<HttpControllerDescriptor, ApiDescription> group in apiGroups)
{
    @Html.DisplayFor(m => group, "ApiGroup")
}
@foreach(在apiGroups中对组进行分组)
{
@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")
}
@foreach(i分组组
在apiGroups.OrderBy中(g=>g.Key.GetCustomAttributes().FirstOrDefault().Prefix)
.ThenBy(g=>g.Key.ControllerName))
{
@DisplayFor(m=>group,“ApiGroup”)
}

但是,我得到了一个空引用异常:在上面的LINQ表达式中,
g.Key.GetCustomAttributes().FirstOrDefault()
对于我的所有控制器都是空的。这对我来说没有任何意义,因为路由本身工作正常(包括前缀)。有什么建议吗?

最近也有同样的问题。。。LINQ表达式中有一个小错误

apiGroups.OrderBy(g => g.Key.GetCustomAttributes<RoutePrefixAttribute>().FirstOrDefault().Prefix)
apiGroups.OrderBy(g=>g.Key.GetCustomAttributes().FirstOrDefault().Prefix)
应改为:

apiGroups.OrderBy(g => g.Key.ControllerType.GetCustomAttributes<RoutePrefixAttribute>().FirstOrDefault().Prefix)
apiGroups.OrderBy(g=>g.Key.ControllerType.GetCustomAttributes().FirstOrDefault().Prefix)

无论如何,这对我来说是可行的。

为了详细说明chris的答案,我能够用下面的代码块实现它。我正在使用Web API 2.2(版本5.1.2)


到文件顶部,以便将通用的
GetCustomAttributes
扩展方法放入范围。

WebAPI版本?帮助页面Nuget软件包版本?版本5.1,从这里开始:(Nuget提要:)。通常我不喜欢使用夜间构建,但在这种情况下,它们有一个我需要的必要特性(复杂类型参数的文档)。如果有帮助,该版本的源代码如下:
@foreach (IGrouping<HttpControllerDescriptor, ApiDescription> group in apiGroups
    .OrderBy(g => g.Key.ControllerType.GetCustomAttributes<System.Web.Http.RoutePrefixAttribute>().FirstOrDefault().Prefix)
    .ThenBy(g => g.Key.ControllerName))
{
    @Html.DisplayFor(m => group, "ApiGroup")
}
@using System.Reflection