清单在多个组API方法 [英] Listing API Methods Under Multiple Groups

查看:439
本文介绍了清单在多个组API方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有Swashbuckle注明code,看起来像这样:

I have Swashbuckle annotated code that looks like this:

[Route("api/Subscribers/{id}/[controller]")]
[Route("api/Organizations/{id}/[controller]")]
public class AddressesController : Controller
{
    [HttpGet("{aid}")]
    [SwaggerResponse(HttpStatusCode.OK, Type = typeof(PostalRecord))]
    public async Task<IActionResult> GetAddress(Guid id, Guid aid)
    {
       //do something
    }

我想使用<$c$c>GroupActionsBy定制,如本例所示,但我希望有上述的getAddress 法同时纳入对应于两个途径prefixes两个单独的组图:

I would like to use the GroupActionsBy customization, as shown in this example, but I want to have the above GetAddress method simultaneously included into two separate groups that correspond to the two route prefixes shown:

[Route("api/Subscribers/{id}/[controller]")]
[Route("api/Organizations/{id}/[controller]")]

在换句话说,我想下都上市相同的方法:

In other words, I want the same method to be listed under both:


  • 订阅

  • 组织

如何才能做到这一点?

顺便说一句,我与ASP.NET核心工作( dnx46 )。如果无法做到这一点与ASP.NET Core版本Swashbucklee,那么全CLR(网页API 2.2?)的例子仍然是AP preciated。

Incidentally, I'm working with ASP.NET Core (dnx46). If it is not yet possible to do this with the ASP.NET Core version of Swashbucklee, then a full-CLR (Web API 2.2?) example would still be appreciated.

此外,对于什么,我试图做一个更完整的故事 - 我有一个<一个href=\"http://stackoverflow.com/questions/36522617/route-disambiguation-in-asp-net-core-mvc-6\">separate SO帖子。

Also, for a more complete story of what I'm trying to do - I have a separate SO post.

由@venerik给出的答案让我接近的解决方案。当我申请他的样本code ...

The answer given by @venerik got me close to the solution. When I apply his sample code...

[SwaggerOperation(Tags = new []{"Subscribers", "Organizations"})]

...这将导致上市扬鞭看起来是这样的:

...this causes the Swagger listings to look like this:

在这里输入的形象描述

总之,地址现在端点出现,我想,但是,随着红色箭头表示,他们现在也被交叉上市的标题下;我不希望组织的端点下被列出的认购端点。

In short, the "Addresses" endpoints are now appearing under the headings that I want but, as the red arrow indicates, they are now also being "cross-listed"; I don't want the "Subscribers" endpoint being listed under the "Organizations" endpoint.

我怀疑一个[SwaggerOperationFilter]可能是另一半的答案,如果我可以把它删除交叉列出的条目。我没有玩过这个机制之前。

I'm suspicious that a [SwaggerOperationFilter] might be "the other half" of the answer, if I can make it remove the cross-listed entries. I've not played with that mechanism before.

此外,它是非常不幸的是[SwaggerOperation]只能在方法/措施被应用。我宁愿把它应用到类本身:

Also, it is very unfortunate that [SwaggerOperation] can only be applied on methods/actions. I would rather apply it to the class itself:

[Route("api/Subscribers/{id}/[controller]")]
[Route("api/Organizations/{id}/[controller]")]
[SwaggerOperation(Tags = new []{"Subscribers", "Organizations"})]
public class AddressesController : Controller
{

有没有补救办法呢?

Is there any remedy for this?

推荐答案

@venerik让我在正确的道路上。但是,而不是一个 [SwaggerOperation] 属性,我需要的是一个 [SwaggerOperationFilter] ,就像这样:

@venerik got me on the right path. But instead of a [SwaggerOperation] attribute, what I needed was a [SwaggerOperationFilter], like this:

public class CategorizeFilter : IOperationFilter
{
    public void Apply(Operation operation, OperationFilterContext context)
    {
        string path = context.ApiDescription.RelativePath;
        string segment = path.Split('/')[1];

        if (segment != context.ApiDescription.GroupName)
        {
            operation.Tags = new List<string> { segment };
        }
    }
}

然后我需要装饰我的行动:

Then I just decorate my actions as needed:

[Route("api/Subscribers/{id}/[controller]")]
[Route("api/Organizations/{id}/[controller]")]
public class AddressesController : Controller
{
    [HttpGet("{aid}")]
    [SwaggerOperationFilter(typeof(CategorizeFilter))]
    [SwaggerResponse(HttpStatusCode.OK, Type = typeof(PostalRecord))]
    public async Task<IActionResult> GetAddress(Guid id, Guid aid)
    {
       //do something
    }

因此​​,在地址一类从我扬鞭UI(好!)完全消失,双设定的终点的路线正确组织和认购人群体之间的分歧。完美!

As a consequence, the "Addresses" category completely disappeared from my Swagger UI (good!) and the twin set of endpoint routes are properly divided between "Organizations" and "Subscribers" groups. Perfect!

这篇关于清单在多个组API方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆