我注意到,Web API并未按帮助页面中的任何特定顺序(或至少通过API名称)进行排序。如果可能,我想按名称类别订购。无法在ToLookup上使用OrderBy。以下是默认情况下附带的代码:
@{
// Group APIs by controller
ILookup<string, ApiDescription> apiGroups = Model.ToLookup(api => api.ActionDescriptor.ControllerDescriptor.ControllerName);
}
<div>
<section>
@foreach (var group in apiGroups)
{
@Html.DisplayFor(m => group, "ApiGroup")
}
</section>
</div>
答案 0 :(得分:9)
上面的答案几乎是正确的,它只需要在最后关闭控制器名称。
ILookup<HttpControllerDescriptor, ApiDescription> apiGroups = Model.OrderBy(d => d.ActionDescriptor.ControllerDescriptor.ControllerName).ToLookup(api => api.ActionDescriptor.ControllerDescriptor);
我测试了它,现在一切正常。
答案 1 :(得分:4)
循环中的顺序:
<div>
<section>
@foreach (var group in apiGroups.OrderBy(x => x.Key))
{
@Html.DisplayFor(m => group, "ApiGroup")
}
</section>
</div>
答案 2 :(得分:0)
ILookup<string, ApiDescription> apiGroups = Model.OrderBy(d => d.ActionDescriptor.ControllerDescriptor.ControllerName).ToLookup(api => api.ActionDescriptor.ControllerDescriptor.ControllerName);