【问题标题】:How to show endpoint documentation when my method inherits a base controller当我的方法继承基本控制器时如何显示端点文档
【发布时间】:2018-10-20 11:12:19
【问题描述】:

我有多个类(超过 100 个)继承自我的基类 BaseController。我所有的课程都是他们的逻辑和模型,但格式的响应(200、404、500,...)总是相同的。

但是当我从我的基类继承时,在我的招摇文档中我看到了我的端点,但响应的详细信息不存在。我该怎么做?

public class BaseController : Controller
{
    public BaseController() {}

    [Produces("application/json")]
    [SwaggerResponse(StatusCodes.Status200OK)]
    [SwaggerResponse(StatusCodes.Status404NotFound)]
    protected async Task<IActionResult> Get(int id)
    {
        ...
    }
}

public class MyController : BaseController
{
    [HttpGet("{id}")]
    public async Task<IActionResult> Get(int id)
    {
        return await base.Get(id).ConfigureAwait(false);
    }
}

【问题讨论】:

  • 我认为这超出了swashbuckle ...在您的代码中,您隐藏了从基础获取的get,因此所有这些注释都不会显示...我认为无论如何都没有继承___ 您可以尝试使用IDocumentFilter 在一个位置应用这些“始终相同”的项目
  • @sorcer1 你找到解决方法了吗?
  • 不,我认为这是不可能的! :(

标签: .net-core swagger swashbuckle


【解决方案1】:

正如@Helder Sepulveda 所说,这不仅仅是花言巧语。 我认为您可以使用IActionModelConvention 来模拟继承动作属性。 像这样使用Action.Filters

public class ActionMethodConvention : IActionModelConvention
{
    public void Apply(ActionModel action)
    {
        var actonBaseResponses = new List<SwaggerResponseAttribute>();//some code to get baseAction reflections
        foreach (var attr in actonBaseResponses)
        {
            action.Filters.Add(new Microsoft.AspNetCore.Mvc.ProducesResponseTypeAttribute(actonBaseResponses.StatusCode));
        }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-23
    • 1970-01-01
    相关资源
    最近更新 更多