如何为我的应用程序中的所有控制器设置ProducesResponseType?

问题描述 投票:0回答:1

我有中间件处理所有未处理的异常和返回

public class ErrorResponseModel { 
  public string ErrorMessage { get; set; } 
}

我喜欢将[ProducesResponseType(typeof(ErrorResponseModel), 500)]添加到我的所有控制器中,但我不明白如何做一次(没有复制或为我的所有控制器引入基类)。

asp.net-core swashbuckle
1个回答
2
投票

将其添加为全局过滤器,如下所示:

public void ConfigureServices(IServiceCollection services)
{
    services.AddMvc(o =>
    {
        o.Filters.Add(new ProducesResponseTypeAttribute(typeof(ErrorResponseModel), 500));
    })
}

这与应用于所有控制器基本相同。

© www.soinside.com 2019 - 2024. All rights reserved.